Skip to content

Commit

Permalink
Merge pull request #67 from lucasmenezesds/grammar-unit-tests
Browse files Browse the repository at this point in the history
Moved Grammar unit tests
  • Loading branch information
yui-knk authored Aug 28, 2023
2 parents 410b68e + 2f905d8 commit e82f4da
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 58 deletions.
27 changes: 27 additions & 0 deletions spec/lrama/grammar/code_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
RSpec.describe Lrama::Grammar::Code do
let(:token_class) { Lrama::Lexer::Token }
let(:user_code_token) { token_class.new(type: T::User_code, s_value: "{ code 1 }") }
let(:initial_act_token) { token_class.new(type: T::P_initial_action, s_value: "%initial-action") }

describe "#translated_code" do
context "when the code type is :user_code" do
it "calls #translated_user_code" do
code = described_class.new(type: :user_code, token_code: user_code_token)

expect(code).to receive(:translated_user_code)

code.translated_code
end
end

context "when the code type is :initial_action" do
it "calls #translated_initial_action_code" do
code = described_class.new(type: :initial_action, token_code: initial_act_token)

expect(code).to receive(:translated_initial_action_code)

code.translated_code
end
end
end
end
56 changes: 56 additions & 0 deletions spec/lrama/grammar/symbol_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
RSpec.describe Lrama::Grammar::Symbol do
let(:token_class) { Lrama::Lexer::Token }

describe "#enum_name" do
describe "symbol is accept_symbol" do
it "returns 'YYSYMBOL_YYACCEPT'" do
sym = described_class.new(id: token_class.new(type: token_class::Ident, s_value: "$accept"))
sym.accept_symbol = true

expect(sym.enum_name).to eq("YYSYMBOL_YYACCEPT")
end
end

describe "symbol is eof_symbol" do
it "returns 'YYSYMBOL_YYEOF'" do
sym = described_class.new(id: token_class.new(type: token_class::Ident, s_value: "YYEOF"), alias_name: "\"end of file\"", token_id: 0)
sym.number = 0
sym.eof_symbol = true

expect(sym.enum_name).to eq("YYSYMBOL_YYEOF")
end
end

describe "symbol's token_id is less than 128" do
it "returns 'YYSYMBOL_number_[alias_name_]'" do
sym1 = described_class.new(id: token_class.new(type: token_class::Char, s_value: "'\\\\'"), alias_name: "\"backslash\"", token_id: 92, number: 70, term: true)
sym2 = described_class.new(id: token_class.new(type: token_class::Char, s_value: "'.'"), alias_name: nil, token_id: 46, number: 69, term: true)
sym3 = described_class.new(id: token_class.new(type: token_class::Char, s_value: "'\\n'"), alias_name: nil, token_id: 10, number: 162, term: true)

expect(sym1.enum_name).to eq("YYSYMBOL_70_backslash_")
expect(sym2.enum_name).to eq("YYSYMBOL_69_")
expect(sym3.enum_name).to eq("YYSYMBOL_162_n_")
end
end

describe "symbol includes $ or @" do
it "returns 'YYSYMBOL_number_ref" do
sym1 = described_class.new(id: token_class.new(type: token_class::Ident, s_value: "$@1"), token_id: -1, number: 165, term: false)
sym2 = described_class.new(id: token_class.new(type: token_class::Ident, s_value: "@2"), token_id: -1, number: 166, term: false)

expect(sym1.enum_name).to eq("YYSYMBOL_165_1")
expect(sym2.enum_name).to eq("YYSYMBOL_166_2")
end
end

describe "symbol's token_id is greater than 127" do
it "returns 'YYSYMBOL_number_ref" do
sym1 = described_class.new(id: token_class.new(type: token_class::Ident, s_value: "keyword_class"), alias_name: "\"`class'\"", token_id: 258, number: 3, term: true)
sym2 = described_class.new(id: token_class.new(type: token_class::Ident, s_value: "top_compstmt"), token_id: 166, number: -1, term: false)

expect(sym1.enum_name).to eq("YYSYMBOL_keyword_class")
expect(sym2.enum_name).to eq("YYSYMBOL_top_compstmt")
end
end
end
end
58 changes: 0 additions & 58 deletions spec/lrama/grammar_spec.rb

This file was deleted.

4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
config.expect_with :rspec do |c|
c.syntax = :expect
end

# Allow to limit the run of the specs
# NOTE: Please do not commit the filter option.
# config.filter_run_when_matching :focus
end

def fixture_path(file_name)
Expand Down

0 comments on commit e82f4da

Please sign in to comment.