Skip to content

Commit

Permalink
Added unit tests for compile_grammar and improve coverage for read_le…
Browse files Browse the repository at this point in the history
…xicon functions
  • Loading branch information
fauxneticien committed Mar 14, 2018
1 parent 491352c commit ae718cc
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 11 deletions.
2 changes: 1 addition & 1 deletion R/compile_grammar.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#' # A more complete demo:
#' # 1. Read a lexicon and group lines using zoo:na.locf0()
#' lexicon_df <-
#' system.file("extdata", "error-french.txt", package = "tidylex") %>%
#' system.file("extdata", "error-french.txt", package = "tidylex") %>%
#' read_lexicon(regex = "\\\\([a-z]+)\\s(.*)", into = c("code", "value")) %>%
#' mutate(lx_line = ifelse(code == "lx", line, NA) %>% zoo::na.locf0())
#'
Expand Down
2 changes: 1 addition & 1 deletion docs/articles/tidylex.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/compile_grammar.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions inst/extdata/basic-nearley-grammar.ne
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"
_ -> " "
2 changes: 1 addition & 1 deletion man/compile_grammar.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 77 additions & 0 deletions tests/testthat/test-compile_grammar.R
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))
)

})
27 changes: 20 additions & 7 deletions tests/testthat/test-read_lexicon.R
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
)

})

0 comments on commit ae718cc

Please sign in to comment.