<commit_before>skip_logic_model = (dkobo_xlform)->
  $model  = dkobo_xlform.model
  $mRdsl  = $modelRowDetailsSkipLogic = $model.rowDetailsSkipLogic
  $vRdsl  = $viewRowDetailSkipLogic   = dkobo_xlform.view.rowDetailSkipLogic
  $slh    = $skipLogicHelpers         = dkobo_xlform.helper.skipLogic

  describe 'skip logic factory', () ->
    _factory = new $mRdsl.SkipLogicFactory()

    beforeEach ->
      @addMatchers toBeInstanceOf: (expectedInstance) ->
        actual = @actual
        notText = (if @isNot then " not" else "")
        @message = ->
          "Expected " + actual.constructor.name + notText + " is instance of " + expectedInstance.name

        actual instanceof expectedInstance

      return


    it 'creates a basic skip logic operator', () ->
      operator = _factory.create_operator 'basic', '=', 1

      expect(operator).toBeInstanceOf $mRdsl.SkipLogicOperator
      expect(operator.get('id')).toBe 1
      expect(operator.get('symbol')).toBe '='

    it 'creates a text skip logic operator', () ->
      operator = _factory.create_operator 'text', '=', 1

      expect(operator).toBeInstanceOf $mRdsl.TextOperator
      expect(operator.get('id')).toBe 1
      expect(operator.get('symbol')).toBe '='

    it 'creates an existence skip logic operator', () ->
      operator = _factory.create_operator 'existence', '=', 1

      expect(operator).toBeInstanceOf $mRdsl.ExistenceSkipLogicOperator
      expect(operator.get('id')).toBe 1
      expect(operator.get('symbol')).toBe '='

    it 'creates a select multiple skip logic operator', () ->
      operator = _factory.create_operator 'select_multiple', '=', 1

      expect(operator).toBeInstanceOf $mRdsl.SelectMultipleSkipLogicOperator
      expect(operator.get('id')).toBe 1
      expect(operator.get('symbol')).toBe '='

    it 'creates an empty skip logic operator', () ->
      operator = _factory.create_operator 'empty', '=', 1

      expect(operator).toBeInstanceOf $mRdsl.EmptyOperator
      expect(operator.get('id')).toBe 0
      expect(operator.get('symbol')).toBeUndefined()

    it 'creates a criterion model', () ->
      criterion = _factory.create_criterion_model 'test question', 'test operator'

      expect(criterion).toBeInstanceOf $mRdsl.SkipLogicCriterion

    it 'creates a text response model', () ->
      expect(_factory.create_response_model 'text').toBeInstanceOf $mRdsl.ResponseModel

    it 'creates an integer response model', () ->
      expect(_factory.create_response_model 'integer').toBeInstanceOf $mRdsl.IntegerResponseModel

    it 'creates a decimal response model', () ->
      expect(_factory.create_response_model 'decimal').toBeInstanceOf $mRdsl.DecimalResponseModel

  #*********************************************************************
  #**----------------------------------------------------------------***
  #********************************************************************#

  describe 'skip logic criterion', () ->
    _criterion = null
    _operator = null
    _survey = null
    _factory = null
    _survey = null


    beforeEach () ->
      _survey =
        rows:
          get: sinon.stub().withArgs('test').returns(getType: () -> operators: [1, 2])
      _criterion = new $mRdsl.SkipLogicCriterion

      _operator =
        serialize: sinon.stub().withArgs('test question', 'test value').returns 'test criterion'
      _factory = sinon.stub()

    #*********************************************************************
    #**----------------------------------------------------------------***
    #********************************************************************#

    describe 'serialize method', () ->
      beforeEach () ->
        _criterion._get_question = sinon.stub().returns
          get: sinon.stub().returns(get: sinon.stub().returns 'test question')
          finalize: () ->

        _criterion.set 'question_cid', 'test question'
        _criterion.set 'operator', _operator

      it 'serializes the criterion when response model validation state is true', () ->
        response_model =
          isValid: () -> true
          get: () -> 'test value'

        _criterion.set 'response_value', response_model

        expect(_criterion.serialize()).toBe 'test criterion'
      it 'serializes the criterion when response model validation state is undefined', () ->
        response_model =
          isValid: () -> undefined
          get: () -> 'test value'

        _criterion.set 'response_value', response_model

        expect(_criterion.serialize()).toBe 'test criterion'
      it 'serializes to empty value when response model validation state is false', () ->
        response_model =
          isValid: () -> false

        _criterion.set 'response_value', response_model

        expect(_criterion.serialize()).toBe ''

    #*********************************************************************
    #**----------------------------------------------------------------***
    #********************************************************************#

    describe 'get question', () ->
      it 'returns current question', () ->
        _criterion.survey =
          rows:
            get: sinon.stub().withArgs('test').returns 'success'
        _criterion.set 'question_cid', 'test'

        expect(_criterion._get_question()).toBe 'success'

    #*********************************************************************
    #**----------------------------------------------------------------***
    #********************************************************************#

    describe 'change question', () ->
      beforeEach () ->
        _criterion.survey = _survey
        _criterion._get_question = sinon.stub().returns(get_type: sinon.stub().returns(response_type: 'text', operators: [1, 2]))
        _criterion.change_operator = sinon.spy()
        _criterion.change_response = sinon.spy()

        _criterion.set 'operator',
          get_id: () -> return -1
          get_type: () -> {}
          get_value: () -> -1

        _criterion.set 'response_value',
          get: sinon.stub().withArgs('value').returns('test')
          get_type: sinon.stub().returns('test')

      it 'changes questions cid', () ->
        _criterion.change_question 'test question'

        expect(_criterion.get 'question_cid').toBe 'test question'

      it 'changes current operator if not in new question type', () ->
        _criterion.change_question('test')

        expect(_criterion.change_operator).toHaveBeenCalledWith 1<commit_msg>removed test of criterion._get_question (not helping right now)<commit_after>skip_logic_model = (dkobo_xlform)->
  $model  = dkobo_xlform.model
  $mRdsl  = $modelRowDetailsSkipLogic = $model.rowDetailsSkipLogic
  $vRdsl  = $viewRowDetailSkipLogic   = dkobo_xlform.view.rowDetailSkipLogic
  $slh    = $skipLogicHelpers         = dkobo_xlform.helper.skipLogic

  describe 'skip logic factory', () ->
    _factory = new $mRdsl.SkipLogicFactory()

    beforeEach ->
      @addMatchers toBeInstanceOf: (expectedInstance) ->
        actual = @actual
        notText = (if @isNot then " not" else "")
        @message = ->
          "Expected " + actual.constructor.name + notText + " is instance of " + expectedInstance.name

        actual instanceof expectedInstance

      return


    it 'creates a basic skip logic operator', () ->
      operator = _factory.create_operator 'basic', '=', 1

      expect(operator).toBeInstanceOf $mRdsl.SkipLogicOperator
      expect(operator.get('id')).toBe 1
      expect(operator.get('symbol')).toBe '='

    it 'creates a text skip logic operator', () ->
      operator = _factory.create_operator 'text', '=', 1

      expect(operator).toBeInstanceOf $mRdsl.TextOperator
      expect(operator.get('id')).toBe 1
      expect(operator.get('symbol')).toBe '='

    it 'creates an existence skip logic operator', () ->
      operator = _factory.create_operator 'existence', '=', 1

      expect(operator).toBeInstanceOf $mRdsl.ExistenceSkipLogicOperator
      expect(operator.get('id')).toBe 1
      expect(operator.get('symbol')).toBe '='

    it 'creates a select multiple skip logic operator', () ->
      operator = _factory.create_operator 'select_multiple', '=', 1

      expect(operator).toBeInstanceOf $mRdsl.SelectMultipleSkipLogicOperator
      expect(operator.get('id')).toBe 1
      expect(operator.get('symbol')).toBe '='

    it 'creates an empty skip logic operator', () ->
      operator = _factory.create_operator 'empty', '=', 1

      expect(operator).toBeInstanceOf $mRdsl.EmptyOperator
      expect(operator.get('id')).toBe 0
      expect(operator.get('symbol')).toBeUndefined()

    it 'creates a criterion model', () ->
      criterion = _factory.create_criterion_model 'test question', 'test operator'

      expect(criterion).toBeInstanceOf $mRdsl.SkipLogicCriterion

    it 'creates a text response model', () ->
      expect(_factory.create_response_model 'text').toBeInstanceOf $mRdsl.ResponseModel

    it 'creates an integer response model', () ->
      expect(_factory.create_response_model 'integer').toBeInstanceOf $mRdsl.IntegerResponseModel

    it 'creates a decimal response model', () ->
      expect(_factory.create_response_model 'decimal').toBeInstanceOf $mRdsl.DecimalResponseModel

  #*********************************************************************
  #**----------------------------------------------------------------***
  #********************************************************************#

  describe 'skip logic criterion', () ->
    _criterion = null
    _operator = null
    _survey = null
    _factory = null
    _survey = null


    beforeEach () ->
      _survey =
        rows:
          get: sinon.stub().withArgs('test').returns(getType: () -> operators: [1, 2])
      _criterion = new $mRdsl.SkipLogicCriterion

      _operator =
        serialize: sinon.stub().withArgs('test question', 'test value').returns 'test criterion'
      _factory = sinon.stub()

    #*********************************************************************
    #**----------------------------------------------------------------***
    #********************************************************************#

    describe 'serialize method', () ->
      beforeEach () ->
        _criterion._get_question = sinon.stub().returns
          get: sinon.stub().returns(get: sinon.stub().returns 'test question')
          finalize: () ->

        _criterion.set 'question_cid', 'test question'
        _criterion.set 'operator', _operator

      it 'serializes the criterion when response model validation state is true', () ->
        response_model =
          isValid: () -> true
          get: () -> 'test value'

        _criterion.set 'response_value', response_model

        expect(_criterion.serialize()).toBe 'test criterion'
      it 'serializes the criterion when response model validation state is undefined', () ->
        response_model =
          isValid: () -> undefined
          get: () -> 'test value'

        _criterion.set 'response_value', response_model

        expect(_criterion.serialize()).toBe 'test criterion'
      it 'serializes to empty value when response model validation state is false', () ->
        response_model =
          isValid: () -> false

        _criterion.set 'response_value', response_model

        expect(_criterion.serialize()).toBe ''

    #*********************************************************************
    #**----------------------------------------------------------------***
    #********************************************************************#


    describe 'change question', () ->
      beforeEach () ->
        _criterion.survey = _survey
        _criterion._get_question = sinon.stub().returns(get_type: sinon.stub().returns(response_type: 'text', operators: [1, 2]))
        _criterion.change_operator = sinon.spy()
        _criterion.change_response = sinon.spy()

        _criterion.set 'operator',
          get_id: () -> return -1
          get_type: () -> {}
          get_value: () -> -1

        _criterion.set 'response_value',
          get: sinon.stub().withArgs('value').returns('test')
          get_type: sinon.stub().returns('test')

      it 'changes questions cid', () ->
        _criterion.change_question 'test question'

        expect(_criterion.get 'question_cid').toBe 'test question'

      it 'changes current operator if not in new question type', () ->
        _criterion.change_question('test')

        expect(_criterion.change_operator).toHaveBeenCalledWith 1
{'fraction_non_alphanumeric': 0.2268304354761525, 'fraction_numerical': 0.0027117562609666615, 'mean_word_length': 2.7756699789220116, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 75, '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': '3384286', 'n_tokens_mistral': 3390, 'n_tokens_neox': 3234, 'n_words': 701}