initial commit
This commit is contained in:
parent
6f36c5123e
commit
fb7a5b1cd2
5 changed files with 124 additions and 0 deletions
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
# direnv
|
||||
.envrc
|
||||
.direnv
|
43
flake.lock
Normal file
43
flake.lock
Normal file
|
@ -0,0 +1,43 @@
|
|||
{
|
||||
"nodes": {
|
||||
"flake-utils": {
|
||||
"locked": {
|
||||
"lastModified": 1667395993,
|
||||
"narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=",
|
||||
"owner": "numtide",
|
||||
"repo": "flake-utils",
|
||||
"rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "numtide",
|
||||
"repo": "flake-utils",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1668650906,
|
||||
"narHash": "sha256-JuiYfDO23O8oxUUOmhQflmOoJovyC5G4RjcYQMQjrRE=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "3a86856a13c88c8c64ea32082a851fefc79aa700",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "NixOS",
|
||||
"ref": "nixpkgs-unstable",
|
||||
"repo": "nixpkgs",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"root": {
|
||||
"inputs": {
|
||||
"flake-utils": "flake-utils",
|
||||
"nixpkgs": "nixpkgs"
|
||||
}
|
||||
}
|
||||
},
|
||||
"root": "root",
|
||||
"version": 7
|
||||
}
|
23
flake.nix
Normal file
23
flake.nix
Normal file
|
@ -0,0 +1,23 @@
|
|||
{
|
||||
inputs = {
|
||||
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
|
||||
flake-utils.url = "github:numtide/flake-utils";
|
||||
};
|
||||
|
||||
outputs = { self, nixpkgs, flake-utils }:
|
||||
flake-utils.lib.eachDefaultSystem (system:
|
||||
let
|
||||
pkgs = import nixpkgs { inherit system; };
|
||||
|
||||
runTests = pkgs.writeShellScript "runTests" ''
|
||||
nix eval --impure --expr 'import ./lib.test.nix {}'
|
||||
'';
|
||||
in
|
||||
{
|
||||
apps.tests = {
|
||||
type = "app";
|
||||
program = toString runTests;
|
||||
};
|
||||
|
||||
});
|
||||
}
|
19
lib.nix
Normal file
19
lib.nix
Normal file
|
@ -0,0 +1,19 @@
|
|||
let
|
||||
inherit (builtins) isString isFloat isInt isBool isList concatStringsSep;
|
||||
|
||||
toLua = val:
|
||||
if isList val then toLuaList val
|
||||
else if isString val then toLuaStr val
|
||||
else if isFloat val || isInt val then toString val
|
||||
else if isBool val then toLuaBool val
|
||||
else "";
|
||||
|
||||
toLuaList = val: "{ ${concatStringsSep ", " (map toLua val)} }";
|
||||
|
||||
toLuaStr = val: "'${val}'";
|
||||
|
||||
toLuaBool = val: if val then "true" else "false";
|
||||
in
|
||||
{
|
||||
inherit toLua;
|
||||
}
|
36
lib.test.nix
Normal file
36
lib.test.nix
Normal file
|
@ -0,0 +1,36 @@
|
|||
{ pkgs ? import <nixpkgs> { } }:
|
||||
|
||||
let
|
||||
nix2lua = import ./lib.nix;
|
||||
inherit (nix2lua) toLua;
|
||||
in
|
||||
pkgs.lib.runTests {
|
||||
"test returns an empty string" = {
|
||||
expr = toLua null;
|
||||
expected = "";
|
||||
};
|
||||
"test returns a lua string" = {
|
||||
expr = toLua "hello world";
|
||||
expected = "'hello world'";
|
||||
};
|
||||
"test returns an integer number" = {
|
||||
expr = toLua 10;
|
||||
expected = "10";
|
||||
};
|
||||
"test returns a float number" = {
|
||||
expr = toLua 10.1;
|
||||
expected = "10.100000";
|
||||
};
|
||||
"test returns true" = {
|
||||
expr = toLua true;
|
||||
expected = "true";
|
||||
};
|
||||
"test returns false" = {
|
||||
expr = toLua false;
|
||||
expected = "false";
|
||||
};
|
||||
"test returns array with all primitive types" = {
|
||||
expr = toLua [ "hello" 10 10.1 true ];
|
||||
expected = "{ 'hello', 10, 10.100000, true }";
|
||||
};
|
||||
}
|
Loading…
Reference in a new issue