forked from ianstormtaylor/superstruct
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not gather tests with file system operations
- Loading branch information
Showing
186 changed files
with
2,229 additions
and
1,705 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import os | ||
import re | ||
|
||
|
||
# Function to convert snake_case or kebab-case to camelCase | ||
def snake_to_camel_case(name): | ||
parts = re.split(r"[_-]", name) | ||
return parts[0] + "".join(x.title() for x in parts[1:]) | ||
|
||
|
||
# Function to process each file | ||
def process_file(file_path): | ||
print(f"Processing file: {file_path}") | ||
|
||
# Read original file content | ||
with open(file_path, "r") as f: | ||
lines = f.readlines() | ||
|
||
# Get filename without extension and convert to camelCase | ||
filename = os.path.basename(file_path) | ||
filename_without_ext = os.path.splitext(filename)[0] | ||
camel_case_name = snake_to_camel_case(filename_without_ext) | ||
|
||
# Add import statement | ||
import_statement = "import {Test} from '../../types'\n" | ||
|
||
# Start building modified content | ||
modified_content = import_statement | ||
found_export_const = False | ||
inside_export_block = False | ||
export_const_lines = [] | ||
|
||
# Process each line | ||
for line in lines: | ||
if not found_export_const and line.startswith("export const"): | ||
found_export_const = True | ||
|
||
if found_export_const: | ||
if line.startswith("export const"): | ||
# Step 1: Remove "export const" | ||
modified_line = line.replace("export const", "", 1) | ||
|
||
# Step 2: Replace "=" with ":" | ||
modified_line = re.sub(r"\s*=\s*", ": ", modified_line) | ||
|
||
# Step 3: Check and adjust line ending | ||
if modified_line.rstrip().endswith("{"): | ||
inside_export_block = True | ||
modified_line = modified_line.rstrip()[:-1] + " {\n" | ||
elif inside_export_block and modified_line.lstrip().startswith("}"): | ||
inside_export_block = False | ||
modified_line = modified_line.rstrip() + ",\n" | ||
else: | ||
modified_line = modified_line.rstrip() + ",\n" | ||
|
||
# Store the modified line | ||
export_const_lines.append(modified_line) | ||
elif inside_export_block: | ||
# Indent and format lines inside the export const block | ||
if line.strip() and not line.strip().startswith("//"): | ||
# Check if line is an object line | ||
if re.match(r"^\s*[\w$]+:\s*{", line.strip()): | ||
export_const_lines.append(f" {line.strip()}\n") | ||
else: | ||
export_const_lines.append(f" {line.strip()}\n") | ||
if line.strip().endswith("}"): | ||
export_const_lines[-1] += "," # Add comma after closing brace | ||
else: | ||
# Append unchanged line to content before finding export const | ||
modified_content += line | ||
else: | ||
# Append unchanged line to content before finding export const | ||
modified_content += line | ||
|
||
if found_export_const: | ||
# Construct the final content | ||
modified_content += f"\nexport const {camel_case_name}Test: Test = {{\n" | ||
modified_content += "".join(export_const_lines) | ||
modified_content += "}\n" | ||
|
||
# Write modified content back to the file | ||
with open(file_path, "w") as f: | ||
f.write(modified_content) | ||
|
||
print(f"File processed: {file_path}") | ||
else: | ||
print(f"No 'export const' found in file: {file_path}") | ||
|
||
|
||
# Main function to recursively process all .ts files in ./test/validation directory | ||
def main(): | ||
base_dir = os.getcwd() # Get current working directory | ||
directory = os.path.join(base_dir, "test", "validation") | ||
|
||
transformed_count = 0 | ||
# Recursively walk through directory and subdirectories | ||
for root, _, files in os.walk(directory): | ||
for filename in files: | ||
if filename.endswith(".ts"): | ||
file_path = os.path.join(root, filename) | ||
process_file(file_path) | ||
transformed_count += 1 | ||
|
||
print(f"Transformation completed. Total files transformed: {transformed_count}") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
am on omit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export type Test = Partial<{ | ||
Struct: any | ||
data: any | ||
create: any | ||
only: any | ||
skip: any | ||
output: any | ||
failures: any | ||
}> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import { Test } from '../../types' | ||
import { any } from '../../../src' | ||
|
||
export const Struct = any() | ||
|
||
export const data = 1 | ||
|
||
export const output = 1 | ||
export const validNumberTest: Test = { | ||
Struct: any(), | ||
data: 1, | ||
output: 1, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import { Test } from '../../types' | ||
import { any } from '../../../src' | ||
|
||
export const Struct = any() | ||
|
||
export const data = 'valid' | ||
|
||
export const output = 'valid' | ||
export const validStringTest: Test = { | ||
Struct: any(), | ||
data: 'valid', | ||
output: 'valid', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import { Test } from '../../types' | ||
import { any } from '../../../src' | ||
|
||
export const Struct = any() | ||
|
||
export const data = undefined | ||
|
||
export const output = undefined | ||
export const validUndefinedTest: Test = { | ||
Struct: any(), | ||
data: undefined, | ||
output: undefined, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,18 @@ | ||
import { Test } from '../../types' | ||
import { array, object, string } from '../../../src' | ||
|
||
export const Struct = array(object({ id: string() })) | ||
const data = [{ id: '1' }, { id: false }, { id: '3' }] | ||
|
||
export const data = [{ id: '1' }, { id: false }, { id: '3' }] | ||
|
||
export const failures = [ | ||
{ | ||
value: false, | ||
type: 'string', | ||
refinement: undefined, | ||
path: [1, 'id'], | ||
branch: [data, data[1], data[1].id], | ||
}, | ||
] | ||
export const invalidElementPropertyTest: Test = { | ||
Struct: array(object({ id: string() })), | ||
data, | ||
failures: [ | ||
{ | ||
value: false, | ||
type: 'string', | ||
refinement: undefined, | ||
path: [1, 'id'], | ||
branch: [data, data[1], data[1].id], | ||
}, | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,18 @@ | ||
import { Test } from '../../types' | ||
import { array, number } from '../../../src' | ||
|
||
export const Struct = array(number()) | ||
const data = [1, 'invalid', 3] | ||
|
||
export const data = [1, 'invalid', 3] | ||
|
||
export const failures = [ | ||
{ | ||
value: 'invalid', | ||
type: 'number', | ||
refinement: undefined, | ||
path: [1], | ||
branch: [data, data[1]], | ||
}, | ||
] | ||
export const invalidElementTest: Test = { | ||
Struct: array(number()), | ||
data, | ||
failures: [ | ||
{ | ||
value: 'invalid', | ||
type: 'number', | ||
refinement: undefined, | ||
path: [1], | ||
branch: [data, data[1]], | ||
}, | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,18 @@ | ||
import { Test } from '../../types' | ||
import { array } from '../../../src' | ||
|
||
export const Struct = array() | ||
const data = 'invalid' | ||
|
||
export const data = 'invalid' | ||
|
||
export const failures = [ | ||
{ | ||
value: 'invalid', | ||
type: 'array', | ||
refinement: undefined, | ||
path: [], | ||
branch: [data], | ||
}, | ||
] | ||
export const invalidOpaqueTest: Test = { | ||
Struct: array(), | ||
data, | ||
failures: [ | ||
{ | ||
value: 'invalid', | ||
type: 'array', | ||
refinement: undefined, | ||
path: [], | ||
branch: [data], | ||
}, | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,18 @@ | ||
import { Test } from '../../types' | ||
import { array, number } from '../../../src' | ||
|
||
export const Struct = array(number()) | ||
const data = 'invalid' | ||
|
||
export const data = 'invalid' | ||
|
||
export const failures = [ | ||
{ | ||
value: 'invalid', | ||
type: 'array', | ||
refinement: undefined, | ||
path: [], | ||
branch: [data], | ||
}, | ||
] | ||
export const invalidTest: Test = { | ||
Struct: array(number()), | ||
data, | ||
failures: [ | ||
{ | ||
value: 'invalid', | ||
type: 'array', | ||
refinement: undefined, | ||
path: [], | ||
branch: [data], | ||
}, | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import { Test } from '../../types' | ||
import { array } from '../../../src' | ||
|
||
export const Struct = array() | ||
|
||
export const data = [1, 'b', true] | ||
|
||
export const output = [1, 'b', true] | ||
export const validOpaqueTest: Test = { | ||
Struct: array(), | ||
data: [1, 'b', true], | ||
output: [1, 'b', true], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import { Test } from '../../types' | ||
import { array, number } from '../../../src' | ||
|
||
export const Struct = array(number()) | ||
|
||
export const data = [1, 2, 3] | ||
|
||
export const output = [1, 2, 3] | ||
export const validTest: Test = { | ||
Struct: array(number()), | ||
data: [1, 2, 3], | ||
output: [1, 2, 3], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,31 @@ | ||
import { Test } from '../../types' | ||
import { object, assign, string, number } from '../../../src' | ||
|
||
const A = object({ a: string() }) | ||
const B = object({ a: number(), b: number() }) | ||
|
||
export const Struct = assign(A, B) | ||
|
||
export const data = { | ||
const data = { | ||
a: 'invalid', | ||
b: 2, | ||
c: 5, | ||
} | ||
|
||
export const failures = [ | ||
{ | ||
value: 'invalid', | ||
type: 'number', | ||
refinement: undefined, | ||
path: ['a'], | ||
branch: [data, data.a], | ||
}, | ||
{ | ||
branch: [data, data.c], | ||
path: ['c'], | ||
refinement: undefined, | ||
type: 'never', | ||
value: 5, | ||
}, | ||
] | ||
export const invalidObjectTest: Test = { | ||
Struct: assign(A, B), | ||
data, | ||
failures: [ | ||
{ | ||
value: 'invalid', | ||
type: 'number', | ||
refinement: undefined, | ||
path: ['a'], | ||
branch: [data, data.a], | ||
}, | ||
{ | ||
branch: [data, data.c], | ||
path: ['c'], | ||
refinement: undefined, | ||
type: 'never', | ||
value: 5, | ||
}, | ||
], | ||
} |
Oops, something went wrong.