pesto: clean up test files

This commit is contained in:
Johannes Kirschbauer 2023-12-17 10:51:08 +01:00 committed by Johannes Kirschbauer
parent a7f5f09bc0
commit 808f67aeba
11 changed files with 248 additions and 73 deletions

View File

@ -80,16 +80,20 @@ pub fn main() {
let data = Pasta::new(&pos_file);
// data.doc_map
let mut json_list: Vec<CompressedDocument> = vec![];
let mut json_list: Vec<Document> = vec![];
for item in data.docs.iter() {
let document = Document::new(&item, &data.doc_map);
let matter = document.meta;
let content = document.content;
let matter = &document.meta;
let content = &document.content;
match opts.format {
Format::DIR => {
if let Some((_, dir)) = item.path.split_last() {
let dir_dest = format!("{}/{}", opts.out, dir.join("/"));
let file_dest = format!("{}/{}.md", opts.out, item.path.join("/"));
let file_dest = format!(
"{}/{}.md",
opts.out,
item.path.join("/").replace("'", "' (Prime)")
);
create_dir_all(dir_dest).unwrap();
let mut file = File::create(file_dest).unwrap();
@ -98,23 +102,15 @@ pub fn main() {
.unwrap();
file.write_all("---\n".as_bytes()).unwrap();
if let Some(content) = content.as_ref().map(|ref i| i.content).flatten() {
file.write_all(dedent(content).as_bytes()).unwrap();
if let Some(content) =
content.as_ref().map(|ref i| i.content.as_ref()).flatten()
{
// let fmt = dedent(content.strip_prefix("\n").unwrap_or(&content));
file.write_all(content.as_bytes()).unwrap();
}
}
}
Format::JSON => json_list.push(CompressedDocument {
m: CompressedDocumentFrontmatter {
al: matter.aliases,
ip: matter.is_primop,
pm: matter.primop_meta.map(|m| CompressedPrimopMatter {
ar: m.args,
ay: m.arity,
}),
pa: matter.path,
},
c: content.map(|c| c.clone()),
}),
Format::JSON => json_list.push(document.clone()),
}
}
if opts.format == Format::JSON {
@ -134,20 +130,22 @@ fn find_document_content<'a>(
item: &'a Docs,
all: &'a HashMap<Rc<ValuePath>, Docs>,
) -> Option<ContentSource<'a>> {
match &item.docs.attr.content {
let content = match &item.docs.attr.content {
Some(ref c) if !c.is_empty() => Some(ContentSource {
content: Some(c),
content: Some(dedent(c)),
source: Some(SourceOrigin {
position: item.docs.attr.position.as_ref(),
path: Some(&item.path),
pos_type: Some(PositionType::Attribute),
}),
}),
// _ if item. item.lambda_content().is_some() => item.lambda_content(),
_ => match item.fst_alias_content(&all) {
Some(d) => Some(d),
None => item.lambda_content(),
},
}
};
return content;
}
#[derive(Serialize, Debug, Clone)]
@ -158,6 +156,7 @@ struct Document<'a> {
#[derive(Serialize, Debug, Clone)]
struct DocumentFrontmatter<'a> {
title: String,
path: &'a Rc<ValuePath>,
aliases: Option<&'a AliasList>,
/// If an item is primop then it should have the PrimopMeta field.
@ -182,11 +181,8 @@ impl<'a> FromDocs<'a> for Document<'a> {
let content = find_document_content(item, &data);
Self {
meta: DocumentFrontmatter {
// content_position: content
// .as_ref()
// .map(|t| t.source.as_ref().map(|s| s.position).flatten())
// .flatten(),
content_meta: content.as_ref().map(|inner| inner.source.clone()).flatten(),
title: item.path.join(".").replace("'", "' (Prime)"),
path: &item.path,
aliases: item.aliases.as_ref(),
attr_position: item.docs.attr.position.as_ref(),

View File

@ -9,6 +9,7 @@ use std::{
};
use serde::{Deserialize, Serialize};
use textwrap::dedent;
use crate::position::FilePosition;
@ -52,7 +53,7 @@ pub struct Docs {
#[derive(Serialize, Debug, Clone)]
pub struct ContentSource<'a> {
pub content: Option<&'a String>,
pub content: Option<String>,
pub source: Option<SourceOrigin<'a>>,
}
@ -96,7 +97,7 @@ impl<'a> Lookups<'a> for Docs {
.map(|i| {
if i.countApplied == Some(0) || (i.countApplied == None && i.isPrimop) {
Some(ContentSource {
content: i.content.as_ref(),
content: i.content.as_ref().map(|inner| dedent(inner)),
source: Some(SourceOrigin {
position: i.position.as_ref(),
path: Some(&self.path),
@ -116,13 +117,20 @@ impl<'a> Lookups<'a> for Docs {
) -> Option<ContentSource<'a>> {
match &self.aliases {
Some(aliases) => {
let x = aliases
.iter()
.find_map(|alias_path| {
data.get(alias_path).map(|i| {
if i.docs.attr.content.is_some() {
let x = aliases.iter().find_map(|alias_path| {
let alias_docs = data
.get(alias_path)
.map(|i| {
if i.docs.attr.content.is_some()
&& !i.docs.attr.content.as_ref().unwrap().is_empty()
{
Some(ContentSource {
content: i.docs.attr.content.as_ref(),
content: i
.docs
.attr
.content
.as_ref()
.map(|inner| dedent(inner)),
source: Some(SourceOrigin {
position: i.docs.attr.position.as_ref(),
path: Some(&i.path),
@ -134,8 +142,9 @@ impl<'a> Lookups<'a> for Docs {
None
}
})
})
.flatten();
.flatten();
alias_docs
});
x
}
_ => None,

View File

@ -38,6 +38,49 @@ mod tests {
}
}
fn docs_to_test_content(data: &Pasta) -> Vec<TestContent> {
let contents: Vec<TestContent> = data
.docs
.iter()
.map(|ref i| {
let document = &Document::new(i, &data.doc_map);
return TestContent {
name: document.meta.path.join("."),
content: document
.content
.as_ref()
.map(|inner| inner.content.as_ref().map(|i| i.clone()))
.flatten(),
};
})
.collect();
return contents;
}
fn docs_to_test_source(data: &Pasta) -> Vec<TestSource> {
let contents: Vec<TestSource> = data
.docs
.iter()
.map(|ref i| {
let document = &Document::new(i, &data.doc_map);
return TestSource {
name: document.meta.path.join("."),
source: document
.content
.as_ref()
.map(|inner| {
inner
.source
.as_ref()
.map(|i| i.path.map(|p| p.join(".")))
.flatten()
})
.flatten(),
};
})
.collect();
return contents;
}
#[test]
fn test_atoms() {
dir_tests("atom", "nix", |path| {
@ -78,42 +121,31 @@ mod tests {
}
#[derive(Serialize, Debug)]
struct TestContent {
struct TestSource {
name: String,
content: Option<String>,
source: Option<String>,
}
#[test]
fn test_content_inheritance() {
dir_tests("inheritance", "json", |path| {
let data: Pasta = Pasta::new(&PathBuf::from(path));
let contents: Vec<TestContent> = data
.docs
.into_iter()
.map(|ref i| {
let document = &Document::new(i, &data.doc_map);
return TestContent {
name: document.meta.path.join("."),
content: document
.content
.as_ref()
.map(|inner| inner.content.map(|i| i.clone()))
.flatten(),
source: document
.content
.as_ref()
.map(|inner| {
inner
.source
.as_ref()
.map(|i| i.path.map(|p| p.join(".")))
.flatten()
})
.flatten(),
};
})
.collect();
let sources = docs_to_test_source(&data);
serde_json::to_string_pretty(&sources).unwrap()
})
}
#[derive(Serialize, Debug)]
struct TestContent {
name: String,
content: Option<String>,
}
#[test]
fn test_content_format() {
dir_tests("content_format", "json", |path| {
let data: Pasta = Pasta::new(&PathBuf::from(path));
let contents = docs_to_test_content(&data);
serde_json::to_string_pretty(&contents).unwrap()
})
}
@ -122,7 +154,6 @@ mod tests {
fn test_bulk() {
dir_tests("bulk", "json", |path| {
let data: Pasta = Pasta::new(&PathBuf::from(path));
serde_json::to_string_pretty(&data.docs[0..10]).unwrap()
})
}

View File

@ -0,0 +1,14 @@
[
{
"name": "lib.add",
"content": "\nReturn the sum of the numbers *e1* and *e2*.\n"
},
{
"name": "lib.trivial.add",
"content": "\nReturn the sum of the numbers *e1* and *e2*.\n"
},
{
"name": "builtins.add",
"content": "\nReturn the sum of the numbers *e1* and *e2*.\n"
}
]

View File

@ -0,0 +1,56 @@
[
{
"docs": {
"attr": {
"position": {
"column": 23,
"file": "test_data/assets/default.nix",
"line": 68
}
},
"lambda": {
"args": ["e1", "e2"],
"arity": 2,
"content": "\n Return the sum of the numbers *e1* and *e2*.\n ",
"experimental": false,
"isPrimop": true,
"name": "add",
"position": null
}
},
"path": ["lib", "add"]
},
{
"docs": {
"attr": {
"position": {
"column": 21,
"file": "test_data/assets/trivial.nix",
"line": 269
}
},
"lambda": {
"args": ["e1", "e2"],
"arity": 2,
"content": "\n Return the sum of the numbers *e1* and *e2*.\n ",
"experimental": false,
"isPrimop": true,
"name": "add",
"position": null
}
},
"path": ["lib", "trivial", "add"]
},
{
"docs": {
"attr": { "content": "", "position": null },
"lambda": {
"content": "\n Return the sum of the numbers *e1* and *e2*.\n ",
"countApplied": 0,
"isPrimop": true,
"position": null
}
},
"path": ["builtins", "add"]
}
]

View File

@ -1,17 +1,14 @@
[
{
"name": "lib.add",
"content": "\n Return the sum of the numbers *e1* and *e2*.\n ",
"source": "lib.add"
},
{
"name": "lib.trivial.add",
"content": "\n Return the sum of the numbers *e1* and *e2*.\n ",
"source": "lib.trivial.add"
},
{
"name": "builtins.add",
"content": "\n Return the sum of the numbers *e1* and *e2*.\n ",
"source": "builtins.add"
}
]

View File

@ -0,0 +1,14 @@
[
{
"name": "builtins.catAttrs",
"source": "lib.attrsets.catAttrs"
},
{
"name": "lib.catAttrs",
"source": "lib.attrsets.catAttrs"
},
{
"name": "lib.attrsets.catAttrs",
"source": "lib.attrsets.catAttrs"
}
]

View File

@ -0,0 +1,64 @@
[
{
"docs": {
"attr": {
"position": null
},
"lambda": {
"args": ["attr", "list"],
"arity": 2,
"content": "\n Collect each attribute named *attr* from a list of attribute\n sets. Attrsets that don't contain the named attribute are\n ignored. For example,\n\n ```nix\n builtins.catAttrs \"a\" [{a = 1;} {b = 0;} {a = 2;}]\n ```\n\n evaluates to `[1 2]`.\n ",
"countApplied": 0,
"experimental": false,
"isPrimop": true,
"name": "catAttrs",
"position": null
}
},
"path": ["builtins", "catAttrs"]
},
{
"docs": {
"attr": {
"position": {
"column": 28,
"file": "/nix/store/knnp4h12pk09vfn18lrrrnh54zsvw3ba-source/lib/default.nix",
"line": 82
}
},
"lambda": {
"args": ["attr", "list"],
"arity": 2,
"content": "\n Collect each attribute named *attr* from a list of attribute\n sets. Attrsets that don't contain the named attribute are\n ignored. For example,\n\n ```nix\n builtins.catAttrs \"a\" [{a = 1;} {b = 0;} {a = 2;}]\n ```\n\n evaluates to `[1 2]`.\n ",
"countApplied": 0,
"experimental": false,
"isPrimop": true,
"name": "catAttrs",
"position": null
}
},
"path": ["lib", "catAttrs"]
},
{
"docs": {
"attr": {
"position": {
"column": 3,
"file": "/nix/store/knnp4h12pk09vfn18lrrrnh54zsvw3ba-source/lib/attrsets.nix",
"line": 398
}
},
"lambda": {
"args": ["attr", "list"],
"arity": 2,
"content": "\n Collect each attribute named *attr* from a list of attribute\n sets. Attrsets that don't contain the named attribute are\n ignored. For example,\n\n ```nix\n builtins.catAttrs \"a\" [{a = 1;} {b = 0;} {a = 2;}]\n ```\n\n evaluates to `[1 2]`.\n ",
"countApplied": 0,
"experimental": false,
"isPrimop": true,
"name": "catAttrs",
"position": null
}
},
"path": ["lib", "attrsets", "catAttrs"]
}
]

View File

@ -1,7 +1,6 @@
[
{
"name": "pkgs.lib.strings.concatStrings",
"content": "\n Concatenate a list of strings.\n\n # Example\n\n ```nix\n concatStrings [\"foo\" \"bar\"]\n => \"foobar\"\n ```\n\n # Type\n\n ```\n concatStrings :: [string] -> string\n ```\n ",
"source": "pkgs.lib.strings.concatStrings"
}
]

View File

@ -1,17 +1,14 @@
[
{
"name": "lib.foldl'",
"content": "\n Reduce a list by applying a binary operator from left to right,\n starting with an initial accumulator.\n Before each application of the operator, the accumulator value is evaluated.\n This behavior makes this function stricter than [`foldl`](#function-library-lib.lists.foldl).\n Unlike [`builtins.foldl'`](https://nixos.org/manual/nix/unstable/language/builtins.html#builtins-foldl'),\n the initial accumulator argument is evaluated before the first iteration.\n A call like\n ```nix\n foldl' op acc₀ [ x₀ x₁ x₂ ... xₙ₋₁ xₙ ]\n ```\n is (denotationally) equivalent to the following,\n but with the added benefit that `foldl'` itself will never overflow the stack.\n ```nix\n let\n acc₁ = builtins.seq acc₀ (op acc₀ x₀ );\n acc₂ = builtins.seq acc₁ (op acc₁ x₁ );\n acc₃ = builtins.seq acc₂ (op acc₂ x₂ );\n ...\n accₙ = builtins.seq accₙ₋₁ (op accₙ₋₁ xₙ₋₁);\n accₙ₊₁ = builtins.seq accₙ (op accₙ xₙ );\n in\n accₙ₊₁\n # Or ignoring builtins.seq\n op (op (... (op (op (op acc₀ x₀) x₁) x₂) ...) xₙ₋₁) xₙ\n ```\n\n # Example\n\n ```nix\n foldl' (acc: x: acc + x) 0 [1 2 3]\n => 6\n ```\n\n # Type\n\n ```\n foldl' :: (acc -> x -> acc) -> acc -> [x] -> acc\n ```\n\n # Arguments\n\n - [op] The binary operation to run, where the two arguments are:\n\n1. `acc`: The current accumulator value: Either the initial one for the first iteration, or the result of the previous iteration\n2. `x`: The corresponding list element for this iteration\n - [acc] The initial accumulator value\n - [list] The list to fold\n\n ",
"source": "lib.lists.foldl'"
},
{
"name": "lib.lists.foldl'",
"content": "\n Reduce a list by applying a binary operator from left to right,\n starting with an initial accumulator.\n Before each application of the operator, the accumulator value is evaluated.\n This behavior makes this function stricter than [`foldl`](#function-library-lib.lists.foldl).\n Unlike [`builtins.foldl'`](https://nixos.org/manual/nix/unstable/language/builtins.html#builtins-foldl'),\n the initial accumulator argument is evaluated before the first iteration.\n A call like\n ```nix\n foldl' op acc₀ [ x₀ x₁ x₂ ... xₙ₋₁ xₙ ]\n ```\n is (denotationally) equivalent to the following,\n but with the added benefit that `foldl'` itself will never overflow the stack.\n ```nix\n let\n acc₁ = builtins.seq acc₀ (op acc₀ x₀ );\n acc₂ = builtins.seq acc₁ (op acc₁ x₁ );\n acc₃ = builtins.seq acc₂ (op acc₂ x₂ );\n ...\n accₙ = builtins.seq accₙ₋₁ (op accₙ₋₁ xₙ₋₁);\n accₙ₊₁ = builtins.seq accₙ (op accₙ xₙ );\n in\n accₙ₊₁\n # Or ignoring builtins.seq\n op (op (... (op (op (op acc₀ x₀) x₁) x₂) ...) xₙ₋₁) xₙ\n ```\n\n # Example\n\n ```nix\n foldl' (acc: x: acc + x) 0 [1 2 3]\n => 6\n ```\n\n # Type\n\n ```\n foldl' :: (acc -> x -> acc) -> acc -> [x] -> acc\n ```\n\n # Arguments\n\n - [op] The binary operation to run, where the two arguments are:\n\n1. `acc`: The current accumulator value: Either the initial one for the first iteration, or the result of the previous iteration\n2. `x`: The corresponding list element for this iteration\n - [acc] The initial accumulator value\n - [list] The list to fold\n\n ",
"source": "lib.lists.foldl'"
},
{
"name": "builtins.foldl'",
"content": "\n Reduce a list by applying a binary operator, from left to right,\n e.g. `foldl' op nul [x0 x1 x2 ...] : op (op (op nul x0) x1) x2)\n ...`. For example, `foldl' (x: y: x + y) 0 [1 2 3]` evaluates to 6.\n The return value of each application of `op` is evaluated immediately,\n even for intermediate values.\n ",
"source": "builtins.foldl'"
}
]

View File

@ -1,12 +1,10 @@
[
{
"name": "lib.strings.concatLines",
"content": "\n Concatenate a list of strings, adding a newline at the end of each one.\n Defined as `concatMapStrings (s: s + \"\\n\")`.\n\n # Example\n\n ```nix\n concatLines [ \"foo\" \"bar\" ]\n => \"foo\\nbar\\n\"\n ```\n\n # Type\n\n ```\n concatLines :: [string] -> string\n ```\n ",
"source": "lib.strings.concatLines"
},
{
"name": "lib.concatLines",
"content": "\n Concatenate a list of strings, adding a newline at the end of each one.\n Defined as `concatMapStrings (s: s + \"\\n\")`.\n\n # Example\n\n ```nix\n concatLines [ \"foo\" \"bar\" ]\n => \"foo\\nbar\\n\"\n ```\n\n # Type\n\n ```\n concatLines :: [string] -> string\n ```\n ",
"source": "lib.strings.concatLines"
}
]