nixite/testing/md.test.nix

214 lines
4.4 KiB
Nix
Raw Normal View History

2023-12-31 00:24:48 +00:00
let
md = import ../nixite/md.nix;
elems = import ../nixite/elems.nix;
it = import ./it.nix;
in with md; [
2023-12-31 00:24:48 +00:00
(it "gets md heading" {
2024-01-01 14:06:54 +00:00
actual = mdBlock "# title of the page";
expected = elems.h1 "title of the page";
})
(it "gets md heading 2" {
2024-01-01 14:06:54 +00:00
actual = mdBlock "## a subheading";
expected = elems.h2 "a subheading";
})
(it "limits to 6 #" {
2024-01-01 14:06:54 +00:00
actual = mdBlock "######## super ultra tiny heading";
expected = elems.h6 "super ultra tiny heading";
})
2023-12-31 00:24:48 +00:00
2024-01-01 17:31:04 +00:00
(it "makes a code block" (let
code = ''
this is my code
'';
in {
actual = mdBlock "```${code}```";
2024-01-01 14:06:54 +00:00
expected = elems.code code;
}))
2024-01-01 17:31:04 +00:00
(it "matches a list of one element" ({
2024-01-01 14:51:59 +00:00
actual = list ''
2024-01-01 17:31:04 +00:00
- something
2024-01-01 14:51:59 +00:00
'';
2024-01-01 17:31:04 +00:00
expected = {
matched = true;
2024-01-02 04:01:19 +00:00
block = elems.List [ "something" ];
2024-01-01 17:31:04 +00:00
};
2024-01-01 20:27:05 +00:00
asJSON = true;
2024-01-01 14:51:59 +00:00
}))
2024-01-01 20:27:05 +00:00
(it "makes a list of many elements" ({
2024-01-01 14:51:59 +00:00
actual = list ''
2024-01-01 17:31:04 +00:00
- something
- something else
2024-01-01 14:51:59 +00:00
'';
2024-01-01 17:31:04 +00:00
expected = {
matched = true;
2024-01-02 04:01:19 +00:00
block = elems.List [ "something" "something else" ];
2024-01-01 17:31:04 +00:00
};
2024-01-01 20:27:05 +00:00
asJSON = true;
}))
(it "makes a list of many checkboxes" ({
actual = list ''
- [ ] something
- [X] something else
'';
expected = {
matched = true;
block = elems.List [
2024-01-02 04:01:19 +00:00
[
(elems.input {
type = "checkbox";
disabled = true;
checked = false;
} "")
"something"
]
[
(elems.input {
type = "checkbox";
disabled = true;
checked = true;
} "")
"something else"
]
2024-01-01 20:27:05 +00:00
];
};
asJSON = true;
2024-01-01 14:51:59 +00:00
}))
2024-01-01 17:31:04 +00:00
(it "matches a list with no whitespace around" ({
actual = list ''
- something
- something else'';
expected = {
matched = true;
2024-01-02 04:01:19 +00:00
block = elems.List [ "something" "something else" ];
2024-01-01 17:31:04 +00:00
};
2024-01-01 20:27:05 +00:00
asJSON = true;
2024-01-01 14:51:59 +00:00
}))
2024-01-01 17:31:04 +00:00
(it "doesnt match not a list" (let str = "blah blah";
in {
2024-01-01 14:51:59 +00:00
actual = list str;
2024-01-01 17:31:04 +00:00
expected = {
matched = false;
block = str;
};
2024-01-01 14:51:59 +00:00
}))
2024-01-01 17:31:04 +00:00
(it "finds surrounded parts" ({
actual = replace (wrap "\\*\\*") elems.strong ''
this text **may** contain **bold** words inside it.
'';
expected = [
"this text"
(elems.strong "may")
"contain"
(elems.strong "bold")
''
words inside it.
''
];
asString = true;
}))
(it "surrounds in list of elems" ({
actual = rule (wrap "\\*") elems.em [
"this text"
(elems.strong "may")
"*or may not* contain"
(elems.strong "bold")
"words *inside* it."
];
expected = [
"this text"
(elems.strong "may")
(elems.em "or may not")
"contain"
(elems.strong "bold")
"words"
(elems.em "inside")
"it."
];
asString = true;
}))
(it "processes whole string with all rules" ({
actual = processStr ''
this text **may** *or may not* contain **bold** words *inside* it.
'';
expected = [
"this text"
(elems.strong "may")
(elems.em "or may not")
"contain"
(elems.strong "bold")
"words"
(elems.em "inside")
''
it.
''
];
asString = true;
2024-01-01 14:51:59 +00:00
}))
(it "processes md block" {
actual = readMd ''
# foo bar
2023-12-31 00:24:48 +00:00
lorem ipsum
'';
expected = [
2024-01-01 06:33:57 +00:00
(elems.h1 { } "foo bar")
""
(elems.p { } ''
2023-12-31 00:24:48 +00:00
lorem ipsum
'')
];
asString = true;
})
2023-12-31 10:33:05 +00:00
(it "can fix file appendixes" {
actual = fixAppendix "index.md";
expected = "index.html";
})
2023-12-31 10:33:05 +00:00
2024-01-01 06:33:57 +00:00
(it "converts markdown to a page" {
2024-01-01 17:31:04 +00:00
actual = toString (mdToPage ./blog/index.md) + "\n";
expected = builtins.readFile ./out/index.html;
2024-01-01 06:33:57 +00:00
asString = true;
})
(it "recursively reads dir" {
actual = recReadMd ./blog;
expected = {
"index.md" = mdToPage ./blog/index.md;
"dir" = { "index.md" = mdToPage ./blog/dir/index.md; };
};
asJSON = true;
})
2023-12-31 10:33:05 +00:00
(it "recursively fixes filename" {
actual = recFixAppendix {
"index.md" = "something";
dir = { "index.md" = "something else"; };
};
expected = {
"index.html" = "something";
dir = { "index.html" = "something else"; };
};
})
2023-12-31 10:33:05 +00:00
(it "recursively translates md to html" {
actual = builtins.toJSON (readDir ./blog);
expected = builtins.toJSON {
"index.html" = mdToPage ./blog/index.md;
"dir" = { "index.html" = mdToPage ./blog/dir/index.md; };
};
})
]