Skip to content

Instantly share code, notes, and snippets.

@slawo-ch
Created September 7, 2017 17:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slawo-ch/8a412cf76015af7963213f2de183692c to your computer and use it in GitHub Desktop.
Save slawo-ch/8a412cf76015af7963213f2de183692c to your computer and use it in GitHub Desktop.
Test for lexing script with and without globals
import org.jcodings.Encoding;
import org.jruby.Ruby;
import org.jruby.common.NullWarnings;
import org.jruby.lexer.ByteListLexerSource;
import org.jruby.lexer.LexerSource;
import org.jruby.lexer.LexingCommon;
import org.jruby.lexer.yacc.RubyLexer;
import org.jruby.parser.ParserConfiguration;
import org.jruby.parser.ParserSupport;
import org.jruby.parser.RubyParserResult;
import org.jruby.util.ByteList;
import org.junit.Test;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
public class LexerTest {
private RubyLexer makeLexer(LexerSource lexerSource){
ParserSupport parserSupport = new ParserSupport();
RubyLexer lexer = new RubyLexer(parserSupport, lexerSource, new NullWarnings(Ruby.getGlobalRuntime()));
parserSupport.setLexer(lexer);
parserSupport.setConfiguration(new ParserConfiguration(Ruby.getGlobalRuntime(), 0, false, true, false));
parserSupport.setResult(new RubyParserResult());
parserSupport.setWarnings(new NullWarnings(Ruby.getGlobalRuntime()));
parserSupport.initTopLocalVariables();
lexer.setState(LexingCommon.EXPR_BEG);
return lexer;
}
public void lexIt(String code, Encoding encoding, Charset charset) throws Exception {
LexerSource lexerSource = new ByteListLexerSource("test", 0, new ByteList(code.getBytes(charset)), null);
lexerSource.setEncoding(encoding);
RubyLexer lexer = makeLexer(lexerSource);
while (!lexer.eofp){
lexer.nextToken();
}
}
@Test
public void test_UTF8_no_globals() throws Exception {
lexIt("puts 'Hello World'", Encoding.load("UTF8"), StandardCharsets.UTF_8);
}
@Test
public void test_UTF8_with_globals() throws Exception {
lexIt("puts $PROGRAM_NAME", Encoding.load("UTF8"), StandardCharsets.UTF_8);
}
@Test
public void test_UTF16LE_no_globals() throws Exception {
lexIt("puts 'Hello World'", Encoding.load("UTF16LE"), StandardCharsets.UTF_16LE);
}
@Test
public void test_UTF16LE_with_globals() throws Exception {
lexIt("puts $PROGRAM_NAME", Encoding.load("UTF16LE"), StandardCharsets.UTF_16LE);
}
@Test
public void test_UTF16BE_no_globals() throws Exception {
lexIt("puts 'Hello World'", Encoding.load("UTF16BE"), StandardCharsets.UTF_16BE);
}
@Test
public void test_UTF16BE_with_globals() throws Exception {
lexIt("puts $PROGRAM_NAME", Encoding.load("UTF16BE"), StandardCharsets.UTF_16BE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment