-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added unit tests for compile_grammar and improve coverage for read_le…
…xicon functions
- Loading branch information
1 parent
491352c
commit ae718cc
Showing
7 changed files
with
110 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Default basic grammar from Nearley playground: | ||
# https://omrelli.ug/nearley-playground/ | ||
MAIN -> SENTENCE "." | ||
SENTENCE -> SUB _ VERB _ MOD | ||
SUB -> "My dog" | "Charles" | "A typical Reddit user" | ||
VERB -> "sleeps" | "thinks" | "cries" | "tweets" | "believes in ponies" | ||
MOD -> "with" _ OBJ | "while thinking about" _ OBJ | "better than" _ OBJ _ "can" | "agressively" | "but" _ SENTENCE | ||
OBJ -> "a hammer" | "nobody" | "snakes" | ||
_ -> " " |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
context("compile_grammar") | ||
|
||
basic_grammar <- | ||
'# Default basic grammar from Nearley playground: | ||
# https://omrelli.ug/nearley-playground/ | ||
MAIN -> SENTENCE "." | ||
SENTENCE -> SUB _ VERB _ MOD | ||
SUB -> "My dog" | "Charles" | "A typical Reddit user" | ||
VERB -> "sleeps" | "thinks" | "cries" | "tweets" | "believes in ponies" | ||
MOD -> "with" _ OBJ | "while thinking about" _ OBJ | "better than" _ OBJ _ "can" | "agressively" | "but" _ SENTENCE | ||
OBJ -> "a hammer" | "nobody" | "snakes" | ||
_ -> " " | ||
' | ||
|
||
test_that("non-string input values rejected by function", { | ||
expect_error(compile_grammar(TRUE)) | ||
expect_error(compile_grammar(154)) | ||
expect_error(compile_grammar(mtcars)) | ||
}) | ||
|
||
test_that("Invalid Nearley grammar throws syntax error", { | ||
expect_error(compile_grammar("MAIN <- SENTENCE")) | ||
}) | ||
|
||
test_that("Nearley .ne grammar files are read in properly", { | ||
|
||
# Can't expect identical since bytecode will be equal but | ||
# will be in different environments | ||
expect_equal( | ||
compile_grammar(basic_grammar), | ||
compile_grammar(system.file("extdata", "basic-nearley-grammar.ne", package = "tidylex")) | ||
) | ||
|
||
}) | ||
|
||
test_that("tests against basic nearley grammar return as expected", { | ||
|
||
expect_identical( | ||
# Correct parse | ||
compile_grammar(basic_grammar)("Charles sleeps while thinking about snakes."), | ||
list( | ||
list( # MAIN | ||
list( # SENTENCE | ||
"Charles", # SUB | ||
" ", # _ | ||
"sleeps", # VERB | ||
" ", # _ | ||
list( # MOD | ||
"while thinking about", # "while thinking about" | ||
" ", # _ | ||
"snakes" # OBJ | ||
) | ||
), | ||
".") # "." | ||
) | ||
) | ||
|
||
expect_error( | ||
# Incomplete parse | ||
compile_grammar(basic_grammar)("Charles sleeps while thinking about ") | ||
) | ||
|
||
expect_error( | ||
# Invalid parse | ||
compile_grammar(basic_grammar)("This test doesn't match :(") | ||
) | ||
|
||
# As above, but error messages returned when told not to stop script | ||
expect_true( | ||
"error" %in% names(compile_grammar(basic_grammar)("Charles sleeps while thinking about ", stop_on_error = FALSE)) | ||
) | ||
|
||
expect_true( | ||
"error" %in% names(compile_grammar(basic_grammar)("This test doesn't match :(", stop_on_error = FALSE)) | ||
) | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,28 @@ | ||
context("read_lexicon") | ||
|
||
check_df <- tibble( | ||
line = 1:5, | ||
data = c("\\lx bonjour", "\\de hello", "", "\\lx au revoir", "\\de goodbye"), | ||
code = c("lx", "de", NA, "lx", "de"), | ||
value = c("bonjour", "hello", NA, "au revoir", "goodbye") | ||
) | ||
|
||
test_file <- system.file("extdata", "mini-french.txt", package = "tidylex") | ||
|
||
test_that("can read mini French lexicon", { | ||
check_df <- tibble( | ||
line = 1:5, | ||
data = c("\\lx bonjour", "\\de hello", "", "\\lx au revoir", "\\de goodbye") | ||
) | ||
|
||
test_file <- system.file("extdata", "mini-french.txt", package = "tidylex") | ||
|
||
|
||
expect_identical( | ||
read_lexicon(file = test_file), | ||
check_df[, 1:2] | ||
) | ||
|
||
}) | ||
|
||
test_that("codes and values are extracted correctly from mini French lexicon", { | ||
|
||
expect_identical( | ||
read_lexicon(file = test_file, regex = "\\\\([a-z]+)\\s(.*)", into = c("code", "value")), | ||
check_df | ||
) | ||
|
||
}) |