-
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.
- Loading branch information
Showing
16 changed files
with
168 additions
and
145 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
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,52 @@ | ||
// Test generation build tool is inspired by this blog post | ||
// https://blog.cyplo.dev/posts/2018/12/generate-rust-tests-from-data/ | ||
|
||
use std::env; | ||
use std::fs::read_dir; | ||
use std::fs::DirEntry; | ||
use std::fs::File; | ||
use std::io::Write; | ||
use std::path::Path; | ||
|
||
// build script's entry point | ||
fn main() { | ||
let out_dir = env::var("OUT_DIR").unwrap(); | ||
let destination = Path::new(&out_dir).join("transpilation_tests.rs"); | ||
let mut test_file = File::create(&destination).unwrap(); | ||
|
||
// write test file header, put `use`, `const` etc there | ||
write_header(&mut test_file); | ||
|
||
let test_data_files = read_dir("./tests/transpilations/").unwrap(); | ||
|
||
for entry in test_data_files { | ||
write_test(&mut test_file, &entry.unwrap()); | ||
} | ||
} | ||
|
||
fn write_test(test_file: &mut File, entry: &DirEntry) { | ||
let directory = entry.path().canonicalize().unwrap(); | ||
let path = directory.display(); | ||
let test_name = format!( | ||
"transpile_{}", | ||
directory.file_stem().unwrap().to_string_lossy() | ||
); | ||
|
||
write!( | ||
test_file, | ||
include_str!("./tests/transpile_template.txt"), | ||
name = test_name, | ||
path = path | ||
) | ||
.unwrap(); | ||
} | ||
|
||
fn write_header(test_file: &mut File) { | ||
write!( | ||
test_file, | ||
r#"use taro::ir::test_utils::utils::final_codegen; | ||
"# | ||
) | ||
.unwrap(); | ||
} |
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
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,4 +1,4 @@ | ||
#[cfg(test)] | ||
//#[cfg(test)] | ||
pub mod utils { | ||
use std::assert_matches::assert_matches; | ||
|
||
|
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,2 @@ | ||
// include tests generated by `build.rs`, one test per directory in tests/data | ||
include!(concat!(env!("OUT_DIR"), "/transpilation_tests.rs")); |
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,5 @@ | ||
let f = () { return true } | ||
let x: Boolean = f() | ||
--- | ||
const f = (() => {return true;}); | ||
const x = f(); |
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,6 @@ | ||
func f() -> Boolean { return true } | ||
let x: Boolean = f() | ||
--- | ||
function f() { | ||
return true;} | ||
const x = f(); |
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,8 @@ | ||
enum IPAddress { | ||
v4(Number, Number, Number, Number) | ||
v6(String) | ||
} | ||
|
||
let ipValue: IPAddress = .v4(192, 168, 0, 1) | ||
--- | ||
const ipValue = [0, [192, 168, 0, 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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
enum IPAddress { | ||
v4(Number, Number, Number, Number) | ||
v6(String) | ||
} | ||
|
||
let ipValue = IPAddress.v4(192, 168, 0, 1) | ||
|
||
--- | ||
|
||
const ipValue = [0, [192, 168, 0, 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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
func f() {}; | ||
f() | ||
--- | ||
function f() { | ||
} | ||
f(); |
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,14 @@ | ||
struct Foo { | ||
let bar: () -> Void | ||
} | ||
|
||
let x = Foo { bar: () {} } | ||
x.bar() | ||
|
||
--- | ||
|
||
function Foo (bar) { | ||
this.bar = bar | ||
} | ||
const x = new Foo((() => {})); | ||
x.bar(); |
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,3 @@ | ||
let val: Number = 23.4 | ||
--- | ||
const val = 23.4; |
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,12 @@ | ||
struct Foo { | ||
let x: Number | ||
} | ||
|
||
let a: Foo = { x: 32 } | ||
|
||
--- | ||
|
||
function Foo (x) { | ||
this.x = x | ||
} | ||
const a = new Foo(32); |
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,16 @@ | ||
struct Test { | ||
let defaultVal = 123 | ||
var noDefault: Boolean | ||
} | ||
|
||
let testVar = Test { noDefault: false } | ||
let val: Number = testVar.defaultVal | ||
|
||
--- | ||
|
||
function Test (defaultVal, noDefault) { | ||
this.defaultVal = defaultVal ?? 123; | ||
this.noDefault = noDefault | ||
} | ||
const testVar = new Test(null, false); | ||
const val = testVar.defaultVal; |
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,5 @@ | ||
let val: (Boolean, Number) = (true, 42) | ||
let val2: Number = val.1 | ||
--- | ||
const val = [true, 42]; | ||
const val2 = val[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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#[test] | ||
fn {name}() {{ | ||
let data = include_str!("{path}"); | ||
|
||
let mut splits = data.split("\n---\n"); | ||
|
||
let before = splits.next().unwrap().trim(); | ||
let after = splits | ||
.next() | ||
.expect("Expected test file to contain line with three dashes (---)") | ||
.trim(); | ||
|
||
assert!( | ||
splits.next() == None, | ||
"Test file contains more than one dash separator" | ||
); | ||
|
||
let transpiled = final_codegen(before).unwrap(); | ||
let transpiled = transpiled.trim(); | ||
|
||
assert_eq!(transpiled, after); | ||
}} |