Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
12ec1e0
Create yaml file with test cases for Int module + generate some tests
OceanOak Oct 23, 2022
4827da7
Fix Rescript and FSharp tests
OceanOak Oct 23, 2022
4d06bb6
Handle exceptions in Rescript and Ocaml
OceanOak Oct 23, 2022
1153343
Handle exceptions in FSharp + generate all int tests in one file
OceanOak Oct 23, 2022
412bff0
Remove json folder
OceanOak Oct 23, 2022
a03e16a
Use a new method to generate tests
OceanOak Oct 24, 2022
3b0cf6e
Clean up code, add remaining Int tests and Bool tests
OceanOak Oct 26, 2022
fcf500a
Add FloatTest functions' yaml files
OceanOak Oct 26, 2022
95285f7
Fix functions that have a string return type
OceanOak Oct 27, 2022
7eb906f
Add Option, Tuple2 and Tuple3 yaml files + add support for array retu…
OceanOak Oct 28, 2022
b67ba17
Add FunTest yaml files
OceanOak Oct 30, 2022
6ce6fca
Add some of ArrayTest's functions yaml files
OceanOak Oct 30, 2022
757940b
Add more ArrayTest's functions yaml files
OceanOak Oct 30, 2022
7b6e9a6
Add more ArrayTest's functions yaml files
OceanOak Oct 31, 2022
addba11
Add StringTest's functions yaml files
OceanOak Oct 31, 2022
e6c2ed4
Add CharTest's functions yaml files
OceanOak Oct 31, 2022
fa590e7
Add CharTest's functions test files
OceanOak Oct 31, 2022
ab120f0
Add the remaining StringTest's functions yaml files
OceanOak Nov 1, 2022
1fe0f59
Add more ArrayTest's functions yaml files
OceanOak Nov 1, 2022
45d86aa
Minor fixes
OceanOak Nov 1, 2022
b24e445
Clean up code + generate tests in the same file
OceanOak Nov 1, 2022
66d9f41
Fix FloatTest's yaml files
OceanOak Nov 1, 2022
9cad9a1
Fix FloatTest's yaml file + generate some Float tests
OceanOak Nov 1, 2022
f8f2173
Add some ListTest's yaml files
OceanOak Nov 1, 2022
64324b7
Add more ListTest's yaml files
OceanOak Nov 2, 2022
7e87528
Generate tests in one file including test header + Delete separate files
OceanOak Nov 2, 2022
b2693b0
Remove unnecessary variables + rename some variables
OceanOak Nov 2, 2022
ed9939d
Minor fixes
OceanOak Nov 2, 2022
e5e7a9e
Add FloatTest and fix FloatTest yaml files
OceanOak Nov 3, 2022
e192e12
Specify whether inputs need labels in the yaml files
OceanOak Nov 4, 2022
32fbbf2
Add labels + Fix some yaml files
OceanOak Nov 7, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,5 @@ _esy
/ocamldoc-json-generator/dune
_opam/

fsharp/src/obj
fsharp/src/bin
.paket/
paket-files/
/json-files
18 changes: 18 additions & 0 deletions bsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "tablecloth",
"version": "0.0.1",
"sources": {
"dir" : "src",
"subdirs" : true
},
"package-specs": {
"module": "commonjs",
"in-source": true
},
"suffix": ".bs.js",
"bs-dependencies": [
],
"warnings": {
"error" : "+101"
}
}
50 changes: 50 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "tablecloth",
"version": "0.0.1",
"scripts": {
"build": "rescript",
"clean": "rescript clean -with-deps",
"start": "rescript build -w"
},
"keywords": [
"rescript"
],
"author": "",
"license": "MIT",
"dependencies": {
"rescript": "*"
},
"devDependencies": {
"@types/node": "^18.11.3"
}
}
248 changes: 248 additions & 0 deletions src/generateTest.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
@module("fs")
external readFileSync: (~name: string, [#utf8]) => string = "readFileSync"

type tests = {
inputs: array<int>,
output: int,
}

type parameter = {
name: string,
\"type": string,
label: bool
}

type data = {
\"module": string,
name: string,
parameters: array<parameter>,
tests: array<tests>,
returnType: string,
}

@scope("JSON") @val
external parseIntoMyData: string => data = "parse"
let \"module" = ref("")
let testCasesR = []
let testCasesF = []
let testCasesO = []
let labeltest = ref([])
let labeltestO = ref([])

// let files = [
// "Float.absolute.json",
// "Float.add.json",
// "Float.clamp.json",
// "Float.atan.json",
// "Float.atan2.json",
// "Float.ceiling.json",
// "Float.cos.json",
// "Float.degrees.json",
// "Float.divide.json",
// "Float.floor.json",
// "Float.fromInt.json",
// "Float.fromString.json",
// "Float.hypotenuse.json",
// "Float.inRange.json",
// "Float.isFinite.json",
// "Float.isInfinite.json",
// "Float.isInteger.json",
// "Float.isNaN.json",
// "Float.log.json",
// "Float.maximum.json",
// "Float.minimum.json",
// "Float.multiply.json",
// "Float.negate.json",
// "Float.power.json",
// "Float.round.json",
// "Float.radians.json",
// "Float.sin.json",
// "Float.squareRoot.json",
// "Float.subtract.json",
// "Float.tan.json",
// "Float.toInt.json",
// "Float.truncate.json",
// "Float.turns.json",
// ]
// let files = [
// "Bool.compare.json",
// "Bool.equal.json",
// "Bool.fromInt.json",
// "Bool.fromString.json",
// "Bool.toInt.json",
// "Bool.toString.json",
// "Bool.xor.json",
// ]
// let files = [
// "Char.fromCode.json",
// "Char.fromString.json",
// "Char.isAlphanumeric.json",
// "Char.isDigit.json",
// "Char.isLetter.json",
// "Char.isLowercase.json",
// "Char.isPrintable.json",
// "Char.isUppercase.json",
// "Char.isWhitespace.json",
// "Char.toCode.json",
// "Char.toDigit.json",
// "Char.toLowercase.json",
// "Char.toString.json",
// "Char.toUppercase.json",
// ]
let files = [
"Int.absolute.json",
"Int.add.json",
"Int.clamp.json",
"Int.divide.json",
"Int.divideFloat.json",
"Int.fromString.json",
"Int.inRange.json",
"Int.isEven.json",
"Int.isOdd.json",
"Int.maximum.json",
"Int.minimum.json",
"Int.modulo.json",
"Int.multiply.json",
"Int.negate.json",
"Int.power.json",
"Int.remainder.json",
"Int.subtract.json",
"Int.toFloat.json",
"Int.toString.json",
]

let generateAllTests = Belt.Array.map(files, file => {
let file = readFileSync(~name="../json-files/" ++ file, #utf8)
let myData = parseIntoMyData(file)
let name = myData.name
let returnType = myData.returnType
\"module" := myData.\"module"
let resultsR = []
let resultsF = []
let resultsO = []
let newInput = ref("")
let newInputR = ref("")
let newInputOF = ref("")
let output = ref("")
let length = Belt.Array.length(myData.parameters)

let generate = Belt.Array.map(myData.tests, test => {
output := Belt.Int.toString(test.output)
if myData.returnType == "string" {
output := `"${Belt.Int.toString(test.output)}"`
} else if Js.String.includes("array", myData.returnType) {
output := `[${Belt.Int.toString(test.output)}]`
//TODO: add a different output for ocaml and f# ([||])
}

if length == 1 {
//param length == 1 type string
if myData.parameters[0].\"type" == "string" {
newInput := `"${Js.Array.toString(test.inputs)}"`
newInputR := Js.Array.toString(test.inputs)
newInputOF := `"${Js.Array.toString(test.inputs)}"`
} //param length == 1 type array
else if Js.Array.isArray(test.inputs) {
newInput := `[${Js.Array.toString(test.inputs)}]`
newInputOF := `[|${Js.Array.toString(test.inputs)}|]`
} else {
//param length == 1

newInput := Js.Array.toString(test.inputs)
newInputR := Js.Array.toString(test.inputs)
newInputOF := Js.Array.toString(test.inputs)
}
} else {
//param length > 1 type
for i in 0 to length - 1{
let label = myData.parameters[i].label
if label{
let testOcaml = `~${myData.parameters[i].name}:${Belt.Int.toString(test.inputs[i])}`
let test = `~${myData.parameters[i].name}=${Belt.Int.toString(test.inputs[i])}`
Belt.Array.push(labeltest.contents,test)
Belt.Array.push(labeltestO.contents,testOcaml)
}else{
Belt.Array.push(labeltest.contents,Belt.Int.toString(test.inputs[i]))
Belt.Array.push(labeltestO.contents,Belt.Int.toString(test.inputs[i]))
}
newInput := Js_array.joinWith(",", labeltest.contents)
newInputOF := Js_array.joinWith(" ", labeltestO.contents)
newInputR := Js_array.joinWith(",", labeltest.contents)
}
labeltest.contents=[]
labeltestO.contents=[]
}

let resultRescript = `test ("${name}(${newInputR.contents})", () => expect(${output.contents != "exception"
? `${\"module".contents}.${name}(${newInput.contents})) |> toEqual(${Js.String.includes(
"Some",
output.contents,
) ||
Js.String.includes("None", output.contents)
? `{open Eq\n${Js.String.slice(
~from=0,
~to_=6,
returnType,
)}(${Js.String.replace(
">",
"",
Js.String.sliceToEnd(~from=7, returnType),
)})}`
: `Eq.${returnType}`}, ${output.contents}))`
: `() => ${\"module".contents}.${name}(${newInput.contents})) |> toThrow)`} \n`
resultsR->Belt.Array.push(resultRescript)

let resultOcaml = `test "${name}(${newInput.contents})" (fun () -> expect (${output.contents != "exception"
? `${\"module".contents}.${name} ${newInputOF.contents}) |> toEqual ${Js.String.includes(
"Some",
output.contents,
) ||
Js.String.includes("None", output.contents)
? `(let open Eq in ${Js.String.slice(
~from=0,
~to_=6,
returnType,
)} ${Js.String.replace(
">",
"",
Js.String.sliceToEnd(~from=7, returnType),
)})`
: `Eq.${returnType}`} ${output.contents == "exception"
? "|> toThrow"
: output.contents}) ; `
: `fun () -> ${name} ${newInputOF.contents}) |> toThrow); `}\n`
resultsO->Belt.Array.push(resultOcaml)

let resultFSharp = `testCase "${name}(${newInput.contents})" \n<| fun _ -> \n ${output.contents != "exception"
? `let expected = ${output.contents}\n Expect.equal expected (${\"module".contents}.${name} ${newInputOF.contents}) "error"`
: `Expect.equal (${\"module".contents}.${name} ${newInputOF.contents}) |> failwith "error"`}\n`
resultsF->Belt.Array.push(resultFSharp)
})

testCasesR->Belt.Array.push(Js.Array.joinWith("", resultsR))
testCasesF->Belt.Array.push(Js.Array.joinWith("", resultsF))
testCasesO->Belt.Array.push(Js.Array.joinWith("", resultsO))
})


let testHeaderR = `open Tablecloth\nopen AlcoJest\n\nlet suite= suite("${\"module".contents}", () => {\n open ${\"module".contents}\n`
let combineTestsR = `${testHeaderR}${Js.Array.joinWith("", testCasesR)}})`
let testHeaderO = `open Tablecloth\nopen AlcoJest\n\nlet suite =\n suite "${\"module".contents}" (fun () ->\n let open ${\"module".contents} in\n`
let combineTestsO = `${testHeaderO}${Js.Array.joinWith("", testCasesO)})`
let testHeaderF = `open Tablecloth\nopen Expecto\n\n[<Tests>]\nlet tests =\n testList\n "${\"module".contents}"\n[`
let combineTestsF = `${testHeaderF}${Js.Array.joinWith("", testCasesF)}]`
Node.Fs.writeFileSync(
`../test/rescriptTests/${\"module".contents}Test.res`,
combineTestsR,
#utf8,
)
Node.Fs.writeFileSync(
`../test/ocamlTests/${\"module".contents}Test.ml`,
combineTestsO,
#utf8,
)
Node.Fs.writeFileSync(
`../test/fsharpTests/${\"module".contents}Test.fs`,
combineTestsF,
#utf8,
)
Loading