<commit_before>describe "mixing", ->

  Model = null
  methods = ["initialize", "get", "set"]

  # Backbone.Model.extend() and Backbone.ComputedFields.mixin are used only in this test, to show what exactly we do.
  # In other tests 'clazz()' shortcut will be used to create new class with already attached mixin
  beforeEach ->
    Model = Backbone.Model.extend()

  it "should override methods in prototype", ->
    Backbone.ComputedFields.mixin Model::
    for method in methods
      expect(Model::[method]).is.not.equal Backbone.Model::[method], "Method #{method} is not overridden in prototype"

  it "should override methods in particular instance", ->
    model = new Model
    Backbone.ComputedFields.mixin model
    for method in methods
      expect(model[method]).is.not.equal Model::[method], "Method '#{method}' is not overridden in instance"
      expect(Model::[method]).is.equal Backbone.Model::[method], "Method '#{method}' is overridden in prototype"<commit_msg>Add tests for mixin initialization logic<commit_after>Model = null

# Backbone.Model.extend() and Backbone.ComputedFields.mixin are used only in these tests, to show what exactly we do.
# In other tests 'clazz()' shortcut will be used to create new class with already attached mixin

describe "mixing", ->

  methods = ["get", "set"]

  beforeEach -> Model = Backbone.Model.extend()

  it "should override methods in prototype", ->
    mixin Model::
    for method in methods
      expect(Model::[method]).is.not.equal Backbone.Model::[method], "Method #{method} is not overridden in prototype"

  it "should override methods in particular instance", ->
    model = new Model
    mixin model
    for method in methods
      expect(model[method]).is.not.equal Model::[method], "Method '#{method}' is not overridden in instance"
      expect(Model::[method]).is.equal Backbone.Model::[method], "Method '#{method}' is overridden in prototype"



describe "initializing", ->

  propName = "_computedFieldsDependenciesMap"

  describe "when initializing with non-empty computed config", ->
    beforeEach ->
      Model = Backbone.Model.extend
        computed:
          answer:
            get: -> 42

    it "should create dependencies config in prototype when attached to prototype", ->
      mixin Model::, yes
      expect(Model::).to.have.ownProperty propName

    it "still should create dependencies config in prototype, even when attached to instance", ->
      model = new Model
      mixin model, yes
      expect(Model::).to.have.ownProperty propName
      expect(model).to.not.have.ownProperty propName

    it "should create dependencies config in instance when instance have own computed config", ->
      model = new Model
      model.computed =
        question:
          get: -> "The meaning of life?"

      mixin model, yes
      expect(model).to.have.ownProperty propName
      expect(Model::).to.not.have.ownProperty propName

    it "by default, should init dependencies config lazy - only after first model creation", ->
      mixin Model::
      expect(Model::).to.not.have.ownProperty propName
      model = new Model
      expect(Model::).to.have.ownProperty propName
      expect(model).to.not.have.ownProperty propName

    it "should initialize dependencies config only once", ->
      mixin Model::, yes
      config1 = Model::[propName]
      model = new Model
      config2 = model[propName]
      expect(config1).is.an "object"
      expect(config1).is.equal config2, "Dependencies config was rewritten after first initialization"

  describe "when initializing with empty computed config", ->

    beforeEach -> Model = Backbone.Model.extend()

    it "should NOT create dependencies config in prototype", ->
      mixin Model::, yes
      expect(Model::).to.not.have.ownProperty propName

    it "should NOT create dependencies config nowhere", ->
      model = new Model
      mixin model, yes
      expect(model).to.not.have.ownProperty propName
      expect(Model::).to.not.have.ownProperty propName

    it "should NOT lazy create dependencies config nowhere", ->
      mixin Model::
      model = new Model
      expect(Model::).to.not.have.ownProperty propName
      expect(model).to.not.have.ownProperty propName
{'fraction_non_alphanumeric': 0.10633270321361059, 'fraction_numerical': 0.0016540642722117202, 'mean_word_length': 3.323799795709908, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 26, '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': '4003516', 'n_tokens_mistral': 1158, 'n_tokens_neox': 1118, 'n_words': 391}