Update parse tests to incorporate module headers

This commit is contained in:
Richard Feldman 2020-11-18 22:33:30 -05:00
parent f5a480f799
commit 3e01df2bcf
8 changed files with 63 additions and 26 deletions

View file

@ -1,4 +1,4 @@
app Main provides [ main ] imports [ Dep1 ] app "test-app" provides [ main ] imports [ Dep1 ]
main : Str main : Str
main = Dep1.value1 {} main = Dep1.value1 {}

View file

@ -1,4 +1,4 @@
app Primary app "primary"
provides [ blah2, blah3, str, alwaysThree, identity, z, w, succeed, withDefault, yay ] provides [ blah2, blah3, str, alwaysThree, identity, z, w, succeed, withDefault, yay ]
imports [ Dep1, Dep2.{ two, foo }, Dep3.Blah.{ bar }, Res ] imports [ Dep1, Dep2.{ two, foo }, Dep3.Blah.{ bar }, Res ]

View file

@ -1,6 +1,7 @@
app Quicksort app "quicksort"
provides [ swap, partition, partitionHelp, quicksort ] packages {}
imports [] imports []
provides [ swap, partition, partitionHelp, quicksort ] to "blah"
quicksort : List (Num a), Int, Int -> List (Num a) quicksort : List (Num a), Int, Int -> List (Num a)
quicksort = \list, low, high -> quicksort = \list, low, high ->

View file

@ -1,4 +1,4 @@
app QuicksortOneDef provides [ quicksort ] imports [] app "quicksort-one-def" provides [ quicksort ] imports []
quicksort = \originalList -> quicksort = \originalList ->
quicksortHelp : List (Num a), Int, Int -> List (Num a) quicksortHelp : List (Num a), Int, Int -> List (Num a)
@ -53,5 +53,5 @@ quicksort = \originalList ->
n = List.len originalList n = List.len originalList
quicksortHelp originalList 0 (n - 1) quicksortHelp originalList 0 (n - 1)

View file

@ -238,31 +238,31 @@ mod test_load {
"RBTree", "RBTree",
indoc!( indoc!(
r#" r#"
interface RBTree exposes [ Dict, empty ] imports [] interface RBTree exposes [ Dict, empty ] imports []
# The color of a node. Leaves are considered Black. # The color of a node. Leaves are considered Black.
NodeColor : [ Red, Black ] NodeColor : [ Red, Black ]
Dict k v : [ Node NodeColor k v (Dict k v) (Dict k v), Empty ] Dict k v : [ Node NodeColor k v (Dict k v) (Dict k v), Empty ]
# Create an empty dictionary. # Create an empty dictionary.
empty : Dict k v empty : Dict k v
empty = empty =
Empty Empty
"# "#
), ),
), ),
( (
"Main", "Main",
indoc!( indoc!(
r#" r#"
app Test provides [ main ] imports [ RBTree ] app "test-app" provides [ main ] imports [ RBTree ]
empty : RBTree.Dict Int Int empty : RBTree.Dict Int Int
empty = RBTree.empty empty = RBTree.empty
main = empty main = empty
"# "#
), ),
), ),
]; ];

View file

@ -174,7 +174,7 @@ pub fn module_name<'a>() -> impl Parser<'a, ModuleName<'a>> {
} }
#[inline(always)] #[inline(always)]
fn app_header<'a>() -> impl Parser<'a, AppHeader<'a>> { pub fn app_header<'a>() -> impl Parser<'a, AppHeader<'a>> {
parser::map( parser::map(
and!( and!(
skip_first!( skip_first!(

View file

@ -12,10 +12,10 @@ pub fn parse_expr_with<'a>(arena: &'a Bump, input: &'a str) -> Result<ast::Expr<
parse_loc_with(arena, input).map(|loc_expr| loc_expr.value) parse_loc_with(arena, input).map(|loc_expr| loc_expr.value)
} }
#[allow(dead_code)]
pub fn parse_header_with<'a>(arena: &'a Bump, input: &'a str) -> Result<ast::Module<'a>, Fail> { pub fn parse_header_with<'a>(arena: &'a Bump, input: &'a str) -> Result<ast::Module<'a>, Fail> {
let state = State::new(input.trim().as_bytes(), Attempting::Module); let state = State::new(input.trim().as_bytes(), Attempting::Module);
let answer = header().parse(arena, state); let answer = header().parse(arena, state);
answer answer
.map(|(loc_expr, _)| loc_expr) .map(|(loc_expr, _)| loc_expr)
.map_err(|(fail, _)| fail) .map_err(|(fail, _)| fail)

View file

@ -21,13 +21,13 @@ mod test_parse {
use roc_parse::ast::CommentOrNewline::*; use roc_parse::ast::CommentOrNewline::*;
use roc_parse::ast::Expr::{self, *}; use roc_parse::ast::Expr::{self, *};
use roc_parse::ast::Pattern::{self, *}; use roc_parse::ast::Pattern::{self, *};
use roc_parse::ast::StrLiteral::*; use roc_parse::ast::StrLiteral::{self, *};
use roc_parse::ast::StrSegment::*; use roc_parse::ast::StrSegment::*;
use roc_parse::ast::{ use roc_parse::ast::{
self, Attempting, Def, EscapedChar, Spaceable, TypeAnnotation, WhenBranch, self, Attempting, Def, EscapedChar, Spaceable, TypeAnnotation, WhenBranch,
}; };
use roc_parse::header::{InterfaceHeader, ModuleName}; use roc_parse::header::{AppHeader, InterfaceHeader, ModuleName};
use roc_parse::module::{interface_header, module_defs}; use roc_parse::module::{app_header, interface_header, module_defs};
use roc_parse::parser::{Fail, FailReason, Parser, State}; use roc_parse::parser::{Fail, FailReason, Parser, State};
use roc_parse::test_helpers::parse_expr_with; use roc_parse::test_helpers::parse_expr_with;
use roc_region::all::{Located, Region}; use roc_region::all::{Located, Region};
@ -2200,7 +2200,43 @@ mod test_parse {
// MODULE // MODULE
#[test] #[test]
fn empty_module() { fn empty_app_header() {
let arena = Bump::new();
let packages = Vec::new_in(&arena);
let imports = Vec::new_in(&arena);
let provides = Vec::new_in(&arena);
let module_name = StrLiteral::PlainLine("test-app");
let expected = AppHeader {
name: Located::new(0, 0, 4, 14, module_name),
packages,
imports,
provides,
to: Located::new(0, 0, 53, 57, "blah"),
after_app_keyword: &[],
before_packages: &[],
after_packages: &[],
before_imports: &[],
after_imports: &[],
before_provides: &[],
after_provides: &[],
before_to: &[],
after_to: &[],
};
let src = indoc!(
r#"
app "test-app" packages {} imports [] provides [] to blah
"#
);
let actual = app_header()
.parse(&arena, State::new(src.as_bytes(), Attempting::Module))
.map(|tuple| tuple.0);
assert_eq!(Ok(expected), actual);
}
#[test]
fn empty_interface_header() {
let arena = Bump::new(); let arena = Bump::new();
let exposes = Vec::new_in(&arena); let exposes = Vec::new_in(&arena);
let imports = Vec::new_in(&arena); let imports = Vec::new_in(&arena);