feat(xod-tabtest, xod-arduino): add support of bytes in tabtests

This commit is contained in:
Kirill Shumilov
2018-05-04 19:32:13 +03:00
parent d1c6a65c2c
commit ebb925c38e
12 changed files with 101 additions and 3 deletions

View File

@@ -9,13 +9,27 @@ module Value = {
| Number(float)
| Boolean(bool)
| String(string)
| Byte(int)
| Invalid(string);
let numberRegex = [%re {|/^[+-]?(?=.)*\d*(?:\.\d+)?$/|}];
let stringRegex = [%re {|/^".*"$/|}];
let byteRegex = [%re {|/^[0-9a-f]{2}h|[0,1]{8}b|\d{1,3}d$/i|}];
let unquote = str =>
str
|> Js.String.replaceByRe([%re "/^\"/"], "")
|> Js.String.replaceByRe([%re "/\"$/"], "");
let init = (str: string) : string =>
String.sub(str, 0, String.length(str) - 1);
let byteStringToInt = str =>
switch (str) {
| bin when Re.test(bin, [%re {|/b$/|}]) =>
"0b" ++ init(bin) |. int_of_string |. (x => Byte(x))
| hex when Re.test(hex, [%re {|/h$/|}]) =>
"0x" ++ init(hex) |. int_of_string |. (x => Byte(x))
| dec when Re.test(dec, [%re {|/d$/|}]) =>
init(dec) |. int_of_string |. (x => Byte(x))
| x => Invalid(x)
};
let parse = str =>
switch (str) {
| "" => Empty
@@ -26,6 +40,8 @@ module Value = {
Number(Js.Float.fromString(numString))
| quotedString when Re.test(quotedString, stringRegex) =>
String(unquote(quotedString))
| byteString when Re.test(byteString, byteRegex) =>
byteStringToInt(byteString)
| x => Invalid(x)
};
};

View File

@@ -16,6 +16,7 @@ module Value: {
| Number(float)
| Boolean(bool)
| String(string)
| Byte(int)
| Invalid(string);
};

View File

@@ -31,6 +31,7 @@ module Probe = {
| Pulse => "pulse"
| Boolean => "boolean"
| Number => "number"
| Byte => "byte"
| String => "string"
}
);

View File

@@ -18,6 +18,7 @@ type dataType =
| Pulse
| Boolean
| Number
| Byte
| String;
type direction =
@@ -50,6 +51,7 @@ let getType = (pin: t) : dataType => {
| "pulse" => Pulse
| "boolean" => Boolean
| "number" => Number
| "byte" => Byte
| "string" => String
| _ =>
Js.Exn.raiseTypeError(

View File

@@ -10,6 +10,7 @@ type dataType =
| Pulse
| Boolean
| Number
| Byte
| String;
let getDirection: t => direction;

View File

@@ -48,17 +48,25 @@ describe("TSV parser", () => {
});
test("recognizes types", () => {
let tsv =
"Number\tBoolean\tString\n"
++ "+.5\ttrue\t\"Hello\"\n"
++ "1.3\tfalse\t\"Some \"quoted\" string\"";
"Number\tBoolean\tByte\tString\n"
++ "+.5\ttrue\t00h\t\"Hello\"\n"
++ "-42\ttrue\t00001101b\t\"World\"\n"
++ "1.3\tfalse\t255d\t\"Some \"quoted\" string\"";
let expected: TabData.t = [
Map.String.empty
|. Map.String.set("Number", Number(0.5))
|. Map.String.set("Boolean", Boolean(true))
|. Map.String.set("Byte", Byte(0))
|. Map.String.set("String", String("Hello")),
Map.String.empty
|. Map.String.set("Number", Number(-42.0))
|. Map.String.set("Boolean", Boolean(true))
|. Map.String.set("Byte", Byte(13))
|. Map.String.set("String", String("World")),
Map.String.empty
|. Map.String.set("Number", Number(1.3))
|. Map.String.set("Boolean", Boolean(false))
|. Map.String.set("Byte", Byte(255))
|. Map.String.set("String", String({|Some "quoted" string|})),
];
expect(TabData.parse(tsv)) |> toEqual(expected);

View File

@@ -0,0 +1,9 @@
struct State {
Number lastValue;
};
{{ GENERATED_CODE }}
void evaluate(Context ctx) {
getState(ctx)->lastValue = getValue<input_VAL>(ctx);
}

View File

@@ -0,0 +1,22 @@
{
"description": "Capturing probe for tabular test framework",
"nodes": [
{
"id": "B1iYZOzoZ",
"position": {
"x": 34,
"y": 102
},
"type": "xod/patch-nodes/not-implemented-in-xod"
},
{
"id": "HkXK-dGob",
"label": "VAL",
"position": {
"x": 34,
"y": 0
},
"type": "xod/patch-nodes/input-byte"
}
]
}

View File

@@ -0,0 +1,7 @@
struct State {};
{{ GENERATED_CODE }}
void evaluate(Context ctx) {
emitValue<output_VAL>(ctx, getValue<output_VAL>(ctx));
}

View File

@@ -0,0 +1,22 @@
{
"nodes": [
{
"boundLiterals": {},
"id": "B1x2RV3eZ",
"label": "VAL",
"position": {
"x": 10,
"y": 120
},
"type": "xod/patch-nodes/output-byte"
},
{
"id": "H1PnRN2lW",
"position": {
"x": 10,
"y": 16
},
"type": "xod/patch-nodes/not-implemented-in-xod"
}
]
}

View File

@@ -0,0 +1,5 @@
COND T F R
false 00h FFh FFh
true 00h FFh 00h
false 32d 42d 42d
true 00001101b 01001100b 00001101b
1 COND T F R
2 false 00h FFh FFh
3 true 00h FFh 00h
4 false 32d 42d 42d
5 true 00001101b 01001100b 00001101b

View File

@@ -0,0 +1,4 @@
COND T F R
false 0.5 1.25 1.25
false 10 20 20
true 10 20 10
1 COND T F R
2 false 0.5 1.25 1.25
3 false 10 20 20
4 true 10 20 10