Sha256: 47abe6359271ed2e3bd135f473338acc970bcda1eb93652f78ba3a60c19eaf8e

Contents?: true

Size: 1.9 KB

Versions: 52

Compression:

Stored size: 1.9 KB

Contents

require 'spec_helper'

RSpec.describe(Regexp::Token) do
  specify('#offset') do
    regexp = /ab?cd/
    tokens = RL.lex(regexp)

    expect(tokens[1].text).to eq 'b'
    expect(tokens[1].offset).to eq [1, 2]

    expect(tokens[2].text).to eq '?'
    expect(tokens[2].offset).to eq [2, 3]

    expect(tokens[3].text).to eq 'cd'
    expect(tokens[3].offset).to eq [3, 5]
  end

  specify('#length') do
    regexp = /abc?def/
    tokens = RL.lex(regexp)

    expect(tokens[0].text).to eq 'ab'
    expect(tokens[0].length).to eq 2

    expect(tokens[1].text).to eq 'c'
    expect(tokens[1].length).to eq 1

    expect(tokens[2].text).to eq '?'
    expect(tokens[2].length).to eq 1

    expect(tokens[3].text).to eq 'def'
    expect(tokens[3].length).to eq 3
  end

  specify('#to_h') do
    regexp = /abc?def/
    tokens = RL.lex(regexp)

    expect(tokens[0].text).to eq 'ab'
    expect(tokens[0].to_h).to eq type: :literal, token: :literal, text: 'ab', ts: 0, te: 2, level: 0, set_level: 0, conditional_level: 0

    expect(tokens[2].text).to eq '?'
    expect(tokens[2].to_h).to eq type: :quantifier, token: :zero_or_one, text: '?', ts: 3, te: 4, level: 0, set_level: 0, conditional_level: 0
  end

  specify('#next') do
    regexp = /a+b?c*d{2,3}/
    tokens = RL.lex(regexp)

    a = tokens.first
    expect(a.text).to eq 'a'

    plus = a.next
    expect(plus.text).to eq '+'

    b = plus.next
    expect(b.text).to eq 'b'

    interval = tokens.last
    expect(interval.text).to eq '{2,3}'

    expect(interval.next).to be_nil
  end

  specify('#previous') do
    regexp = /a+b?c*d{2,3}/
    tokens = RL.lex(regexp)

    interval = tokens.last
    expect(interval.text).to eq '{2,3}'

    d = interval.previous
    expect(d.text).to eq 'd'

    star = d.previous
    expect(star.text).to eq '*'

    c = star.previous
    expect(c.text).to eq 'c'

    a = tokens.first
    expect(a.text).to eq 'a'
    expect(a.previous).to be_nil
  end
end

Version data entries

52 entries across 39 versions & 10 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/regexp_parser-1.8.0/spec/token/token_spec.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/regexp_parser-1.8.0/spec/token/token_spec.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/regexp_parser-1.8.0/spec/token/token_spec.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/regexp_parser-1.8.0/spec/token/token_spec.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/regexp_parser-2.2.0/spec/token/token_spec.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/regexp_parser-1.8.0/spec/token/token_spec.rb
regexp_parser-2.2.0 spec/token/token_spec.rb
tdiary-5.2.0 vendor/bundle/ruby/2.7.0/gems/regexp_parser-2.1.1/spec/token/token_spec.rb
tdiary-5.2.0 vendor/bundle/ruby/3.0.0/gems/regexp_parser-2.1.1/spec/token/token_spec.rb
date_n_time_picker_activeadmin-0.1.2 vendor/bundle/ruby/2.6.0/gems/regexp_parser-2.1.1/spec/token/token_spec.rb
date_n_time_picker_activeadmin-0.1.1 vendor/bundle/ruby/2.6.0/gems/regexp_parser-2.1.1/spec/token/token_spec.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/regexp_parser-1.8.0/spec/token/token_spec.rb
tdiary-5.1.7 vendor/bundle/ruby/3.0.0/gems/regexp_parser-2.1.1/spec/token/token_spec.rb
tdiary-5.1.7 vendor/bundle/ruby/2.7.0/gems/regexp_parser-2.1.1/spec/token/token_spec.rb
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/regexp_parser-2.1.1/spec/token/token_spec.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/regexp_parser-2.1.1/spec/token/token_spec.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/2.7.0/gems/regexp_parser-2.1.1/spec/token/token_spec.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/3.0.0/gems/regexp_parser-2.0.3/spec/token/token_spec.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/3.0.0/gems/tdiary-5.1.4/vendor/bundle/ruby/2.7.0/gems/regexp_parser-1.8.2/spec/token/token_spec.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/3.0.0/gems/regexp_parser-1.8.2/spec/token/token_spec.rb