tests/testthat/test.lex_token5.R

#! /usr/bin/env Rscript

library(testthat)
library(rly)

context("Return a bad token name")

Lexer <- R6::R6Class("Lexer",
  public = list(
    tokens = c('NUMBER', 'PLUS', 'MINUS'),
    t_PLUS = '\\+',
    t_MINUS = '-',
    t_NUMBER = function(re='\\d+', t) {
      t$type = "NUM"
      return(t)
    },
    t_ignore = " \t",
    t_error = function(t) {
      return(t)
    }
  )
)

test_that("bad token type", {
  lexer <- rly::lex(Lexer)
  lexer$input("1234")
  expect_error(lexer$token(), "Rule 't_NUMBER' returned an unknown token type 'NUM'")
})

Try the rly package in your browser

Any scripts or data that you put into this service are public.

rly documentation built on May 8, 2022, 5:05 p.m.