Skip to content

Commit

Permalink
✅ add tests comparing qs_dart and qs for JavaScript (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
techouse authored Apr 14, 2024
1 parent 052ac7b commit 7c2f9a0
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ jobs:
uses: dart-lang/setup-dart@v1
with:
sdk: stable
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: 20
- id: checkout
name: Checkout repository
uses: actions/checkout@v4
Expand All @@ -55,6 +59,24 @@ jobs:
run: |
dart pub global run coverage:test_with_coverage
dart pub global run remove_from_coverage:remove_from_coverage -f coverage/lcov.info -r '\.g\.dart$'
- name: Run a comparison test between qs_dart and qs for JavaScript
continue-on-error: true
working-directory: test/comparison
run: |
set -e
npm install
# Run the JavaScript and Dart scripts and save their outputs
node_output=$(node qs.js)
dart_output=$(dart run qs.dart)
# Compare the outputs
if [ "$node_output" == "$dart_output" ]; then
echo "The outputs are identical."
else
echo "The outputs are different."
exit 1
fi
- name: Upload coverage
uses: codecov/codecov-action@v4
with:
Expand Down
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ dependencies:
weak_map: ^3.0.1

dev_dependencies:
cli_script: ^1.0.0
euc: ^1.0.6+8
lints: ^3.0.0
path: ^1.8.0
test: ^1.25.2

topics:
Expand Down
2 changes: 2 additions & 0 deletions test/comparison/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
package-lock.json
17 changes: 17 additions & 0 deletions test/comparison/compare_outputs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bash

# Get the directory of the script
script_dir=$(dirname "$0")

# Run the JavaScript and Dart scripts and save their outputs
node_output=$(node "$script_dir/qs.js")
dart_output=$(dart run "$script_dir/qs.dart")

# Compare the outputs
if [ "$node_output" == "$dart_output" ]; then
echo "The outputs are identical."
exit 0
else
echo "The outputs are different."
exit 1
fi
10 changes: 10 additions & 0 deletions test/comparison/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "qs_comparison",
"version": "1.0.0",
"description": "A comparison of query string parsing libraries",
"author": "Klemen Tusar",
"license": "BSD-3-Clause",
"dependencies": {
"qs": "^6.12.1"
}
}
26 changes: 26 additions & 0 deletions test/comparison/qs.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'dart:convert' show jsonDecode, jsonEncode;
import 'dart:io' show File, Platform, exitCode, stdout;
import 'package:qs_dart/qs_dart.dart' as qs;
import 'package:cli_script/cli_script.dart' show wrapMain;
import 'package:path/path.dart' as p;

void main() {
wrapMain(() {
exitCode = 0;

final String scriptDir = p.dirname(Platform.script.toFilePath());
final File file = File(p.join(scriptDir, 'test_cases.json'));
final String contents = file.readAsStringSync();
final List<Map<String, dynamic>> e2eTestCases =
List<Map<String, dynamic>>.from(
jsonDecode(contents),
);

for (final testCase in e2eTestCases) {
final String encoded = qs.encode(testCase['data']);
final Map decoded = qs.decode(testCase['encoded']);
stdout.writeln('Encoded: $encoded');
stdout.writeln('Decoded: ${jsonEncode(decoded)}');
}
});
}
14 changes: 14 additions & 0 deletions test/comparison/qs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const fs = require('fs');
const path = require('path');
const qs = require('qs');

// Use path.join to combine __dirname with the relative path to test_cases.json
const filePath = path.join(__dirname, 'test_cases.json');
const e2eTestCases = JSON.parse(fs.readFileSync(filePath).toString());

e2eTestCases.forEach(testCase => {
let encoded = qs.stringify(testCase.data);
let decoded = qs.parse(encoded);
console.log('Encoded: ' + encoded);
console.log('Decoded: ' + JSON.stringify(decoded));
});
78 changes: 78 additions & 0 deletions test/comparison/test_cases.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
[
{
"data": {},
"encoded": ""
},
{
"data": {
"a": "b"
}, "encoded": "a=b"
},
{
"data": {
"a": "b",
"c": "d"
},
"encoded": "a=b&c=d"
},
{
"data": {
"a": "b",
"c": "d",
"e": "f"
},
"encoded": "a=b&c=d&e=f"
},
{
"data": {
"a": "b",
"c": "d",
"e": [
"f",
"g",
"h"
]
},
"encoded": "a=b&c=d&e[0]=f&e[1]=g&e[2]=h"
},
{
"data": {
"a": "b",
"c": "d",
"e": [
"f",
"g",
"h"
],
"i": {
"j": "k",
"l": "m"
}
},
"encoded": "a=b&c=d&e[0]=f&e[1]=g&e[2]=h&i[j]=k&i[l]=m"
},
{
"data": {
"filters": {
"$or": [
{
"date": {
"$eq": "2020-01-01"
}
},
{
"date": {
"$eq": "2020-01-02"
}
}
],
"author": {
"name": {
"$eq": "John Doe"
}
}
}
},
"encoded": "filters[$or][0][date][$eq]=2020-01-01&filters[$or][1][date][$eq]=2020-01-02&filters[author][name][$eq]=John Doe"
}
]

0 comments on commit 7c2f9a0

Please sign in to comment.