Skip to content

Commit

Permalink
Do not gather tests with file system operations
Browse files Browse the repository at this point in the history
  • Loading branch information
yeoffrey committed Jul 4, 2024
1 parent 5d18222 commit b8adb36
Show file tree
Hide file tree
Showing 186 changed files with 2,229 additions and 1,705 deletions.
108 changes: 108 additions & 0 deletions insane.py
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()
1 change: 1 addition & 0 deletions note.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
am on omit
9 changes: 9 additions & 0 deletions test/types.ts
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
}>
11 changes: 6 additions & 5 deletions test/validation/any/valid-number.ts
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,
}
11 changes: 6 additions & 5 deletions test/validation/any/valid-string.ts
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',
}
11 changes: 6 additions & 5 deletions test/validation/any/valid-undefined.ts
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,
}
27 changes: 15 additions & 12 deletions test/validation/array/invalid-element-property.ts
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],
},
],
}
27 changes: 15 additions & 12 deletions test/validation/array/invalid-element.ts
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]],
},
],
}
27 changes: 15 additions & 12 deletions test/validation/array/invalid-opaque.ts
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],
},
],
}
27 changes: 15 additions & 12 deletions test/validation/array/invalid.ts
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],
},
],
}
11 changes: 6 additions & 5 deletions test/validation/array/valid-opaque.ts
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],
}
11 changes: 6 additions & 5 deletions test/validation/array/valid.ts
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],
}
42 changes: 22 additions & 20 deletions test/validation/assign/invalid-object.ts
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,
},
],
}
Loading

0 comments on commit b8adb36

Please sign in to comment.