<commit_before>should = require 'should'

describe 'boolean', ->
  schema = 'leaf foo { type boolean; }'

  it "should validate boolean value", ->
    o = (Yang schema)()
    (-> o.foo = 'yes').should.throw()
    (-> o.foo = 'True').should.throw()
    (-> o.foo = 1).should.throw()
    (-> o.foo = 0).should.throw()
    (-> o.foo = 'true').should.not.throw()
    (-> o.foo = true).should.not.throw()
    (-> o.foo = false).should.not.throw()

  it "should convert input to boolean value", ->
    o = (Yang schema)()
    o.foo = 'true';    o.foo.should.equal(true)
    o.foo = true;      o.foo.should.equal(true)
    o.foo = 'false';   o.foo.should.equal(false)
    o.foo = false;     o.foo.should.equal(false)

describe 'enumeration', ->
  schema = """
    type enumeration {
      enum apple;
      enum orange { value 20; }
      enum banana;
    }
    """
  it "should parse type enumeration statement", ->
    y = Yang.parse schema
    y.should.have.property('tag').and.equal('enumeration')
    y.enum.should.be.instanceOf(Array).and.have.length(3)
    for i in y.enum
      switch i.tag
        when 'apple'
          i.should.have.property('value')
          i.value.tag.should.equal('0')
        when 'orange'
          i.value.tag.should.equal('20')
        when 'banana'
          i.should.have.property('value')
          i.value.tag.should.equal('21')

  it "should validate enum constraint", ->
    o = (Yang "leaf foo { #{schema} }")()
    (-> o.foo = 'lemon').should.throw()
    (-> o.foo = 3).should.throw()
    (-> o.foo = '1').should.throw()
    (-> o.foo = 'apple').should.not.throw()
    (-> o.foo = '0').should.not.throw()
    (-> o.foo = 21).should.not.throw()

describe 'string', ->
  schema = """
    type string {
      length 1..5;
      pattern '^[a-z]+$';
    }
    """
  it "should parse type string statement", ->
    y = Yang.parse schema
    y.should.have.property('tag').and.equal('string')
    y.length.should.have.property('tag').and.equal('1..5')
    y.pattern.should.be.instanceOf(Array).and.have.length(1)

  it "should parse multi-line regexp pattern", ->
    y = Yang.parse """
      type string {
        pattern
          '^[a-z]+'
        + '[0-9]+$';
      }
      """
    y.resolve()
    y.pattern.should.be.instanceof(Array)
    y.pattern[0].should.have.property('tag').and.be.instanceof(RegExp)
    should(y.pattern[0].tag.toString()).equal('/^[a-z]+[0-9]+$/')

  it "should validate length constraint", ->
    o = (Yang "leaf foo { #{schema} }")()
    (-> o.foo = '').should.throw()<commit_msg>added special escape regexp test case for *pattern* statement, see #50<commit_after>should = require 'should'

describe 'boolean', ->
  schema = 'leaf foo { type boolean; }'

  it "should validate boolean value", ->
    o = (Yang schema)()
    (-> o.foo = 'yes').should.throw()
    (-> o.foo = 'True').should.throw()
    (-> o.foo = 1).should.throw()
    (-> o.foo = 0).should.throw()
    (-> o.foo = 'true').should.not.throw()
    (-> o.foo = true).should.not.throw()
    (-> o.foo = false).should.not.throw()

  it "should convert input to boolean value", ->
    o = (Yang schema)()
    o.foo = 'true';    o.foo.should.equal(true)
    o.foo = true;      o.foo.should.equal(true)
    o.foo = 'false';   o.foo.should.equal(false)
    o.foo = false;     o.foo.should.equal(false)

describe 'enumeration', ->
  schema = """
    type enumeration {
      enum apple;
      enum orange { value 20; }
      enum banana;
    }
    """
  it "should parse type enumeration statement", ->
    y = Yang.parse schema
    y.should.have.property('tag').and.equal('enumeration')
    y.enum.should.be.instanceOf(Array).and.have.length(3)
    for i in y.enum
      switch i.tag
        when 'apple'
          i.should.have.property('value')
          i.value.tag.should.equal('0')
        when 'orange'
          i.value.tag.should.equal('20')
        when 'banana'
          i.should.have.property('value')
          i.value.tag.should.equal('21')

  it "should validate enum constraint", ->
    o = (Yang "leaf foo { #{schema} }")()
    (-> o.foo = 'lemon').should.throw()
    (-> o.foo = 3).should.throw()
    (-> o.foo = '1').should.throw()
    (-> o.foo = 'apple').should.not.throw()
    (-> o.foo = '0').should.not.throw()
    (-> o.foo = 21).should.not.throw()

describe 'string', ->
  schema = """
    type string {
      length 1..5;
      pattern '^[a-z]+$';
    }
    """
  it "should parse type string statement", ->
    y = Yang.parse schema
    y.should.have.property('tag').and.equal('string')
    y.length.should.have.property('tag').and.equal('1..5')
    y.pattern.should.be.instanceOf(Array).and.have.length(1)

  it "should parse multi-line regexp pattern", ->
    y = Yang.parse """
      type string {
        pattern
          '^[a-z]+'
        + '[0-9]+$';
      }
      """
    y.resolve()
    y.pattern.should.be.instanceof(Array)
    y.pattern[0].should.have.property('tag').and.be.instanceof(RegExp)
    should(y.pattern[0].tag.toString()).equal('/^[a-z]+[0-9]+$/')

  it "should parse special escape regexp pattern", ->
    y = Yang 'type string { pattern "\\d+"; }'
    y.pattern[0].should.have.property('tag').and.be.instanceof(RegExp)
    y.pattern[0].tag.test(123).should.equal(true)
    y.pattern[0].tag.test('hi').should.equal(false)

  it "should validate length constraint", ->
    o = (Yang "leaf foo { #{schema} }")()
    (-> o.foo = '').should.throw()
{'fraction_non_alphanumeric': 0.20299224233468785, 'fraction_numerical': 0.011082379017362394, 'mean_word_length': 2.3675373134328357, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3596868', 'n_tokens_mistral': 1932, 'n_tokens_neox': 1862, 'n_words': 359}