<commit_before>/*global Backbone, BackboneProxy, QUnit, test, strictEqual, ok, throws */
(function () {
  'use strict';

  var User = Backbone.Model.extend({ defaults: { name: 'Anna', age: 23 } }), user;

  QUnit.module('examples: use cases', {
    setup: function () {
      user = new User();
    }
  });

  test('proxies that log attribute changes', 2, function () {
    var
      nextExpectedLog,
      console = {
        log: function (msg) {
          nextExpectedLog || (nextExpectedLog = 'setting name to Mairy on user');
          strictEqual(msg, nextExpectedLog, 'logged "' + msg + '"');
          nextExpectedLog = 'user name is Mairy';
        }
      };

    ////

    // Create a UserProxy class. Instances will proxy the given user model
    // Note that in this case, user is a model _instance_ - not a class
    var UserProxy = BackboneProxy.extend(user);

    // Instantiate a logging proxy - a proxy that logs all invocations of .set
    var userWithLog = new UserProxy();
    userWithLog.set = function (key, val) {
      console.log('setting ' + key + ' to ' + val + ' on user');

      // Delegate to UserProxy implementation
      UserProxy.prototype.set.apply(this, arguments);
    };

    // Will not log
    user.set('name', 'Norman');

    // Will log 'setting name to Mairy on user'
    userWithLog.set('name', 'Mairy');

    // Will log 'user name is Mairy'
    console.log('user name is ' + user.get('name'));
  });


  test('readonly proxies', 2, function () {

    var alert = function (msg) {
        strictEqual(msg, 'user name was set to Mairy', 'logged expected message');
      };

    ////

    // Create a UserProxy class
    var UserProxy = BackboneProxy.extend(user);

    // Instantiate a readonly proxy - a proxy that throws on any invocation of .set
    var userReadonly = new UserProxy();
    userReadonly.set = function () {
      throw 'cannot set attributes on readonly user model';
    };

    // Attributes cannot be set on userReadonly
    // However attribute changes can be listened for
    userReadonly.on('change:name', function () {
      alert('user name was set to ' + userReadonly.get('name'));
    });

    throws(function () {
      // Will throw
      userReadonly.set('name', 'Mairy');
    }, 'setting readonly proxy throws');

    // Will alert 'user name was set to Mairy'
    user.set('name', 'Mairy');
  });


  test('view-specific proxies', 2, function () {
    var
      nextExpectedLog,
      console = {
        log: function (msg) {
          nextExpectedLog || (nextExpectedLog = 'user modified by view1');
          strictEqual(msg, nextExpectedLog, 'logged "' + msg + '"');
          nextExpectedLog = 'user modified by view2';
        }
      },
      SomeView = Backbone.View.extend({
        modifyModel: function () {
          this.model.set({ name: 'Betty' });
        }
      }),
      SomeOtherView = Backbone.View.extend({
        modifyModel: function () {<commit_msg>Implement dummy-view for example-tests<commit_after>/*global Backbone, BackboneProxy, QUnit, test, strictEqual, ok, throws */
(function () {
  'use strict';

  var
    User = Backbone.Model.extend({ defaults: { name: 'Anna', age: 23 } }), user,
    DummyView = Backbone.View.extend({
      constructor: function DummyView () {
        // _Don't_ ensureElement. This will break the tests in environments where $ is not defined
        this._ensureElement = function () {};
        Backbone.View.apply(this, arguments);
      }
    });

  QUnit.module('examples: use cases', {
    setup: function () {
      user = new User();
    }
  });

  test('proxies that log attribute changes', 2, function () {
    var
      nextExpectedLog,
      console = {
        log: function (msg) {
          nextExpectedLog || (nextExpectedLog = 'setting name to Mairy on user');
          strictEqual(msg, nextExpectedLog, 'logged "' + msg + '"');
          nextExpectedLog = 'user name is Mairy';
        }
      };

    ////

    // Create a UserProxy class. Instances will proxy the given user model
    // Note that in this case, user is a model _instance_ - not a class
    var UserProxy = BackboneProxy.extend(user);

    // Instantiate a logging proxy - a proxy that logs all invocations of .set
    var userWithLog = new UserProxy();
    userWithLog.set = function (key, val) {
      console.log('setting ' + key + ' to ' + val + ' on user');

      // Delegate to UserProxy implementation
      UserProxy.prototype.set.apply(this, arguments);
    };

    // Will not log
    user.set('name', 'Norman');

    // Will log 'setting name to Mairy on user'
    userWithLog.set('name', 'Mairy');

    // Will log 'user name is Mairy'
    console.log('user name is ' + user.get('name'));
  });


  test('readonly proxies', 2, function () {

    var alert = function (msg) {
        strictEqual(msg, 'user name was set to Mairy', 'logged expected message');
      };

    ////

    // Create a UserProxy class
    var UserProxy = BackboneProxy.extend(user);

    // Instantiate a readonly proxy - a proxy that throws on any invocation of .set
    var userReadonly = new UserProxy();
    userReadonly.set = function () {
      throw 'cannot set attributes on readonly user model';
    };

    // Attributes cannot be set on userReadonly
    // However attribute changes can be listened for
    userReadonly.on('change:name', function () {
      alert('user name was set to ' + userReadonly.get('name'));
    });

    throws(function () {
      // Will throw
      userReadonly.set('name', 'Mairy');
    }, 'setting readonly proxy throws');

    // Will alert 'user name was set to Mairy'
    user.set('name', 'Mairy');
  });


  test('view-specific proxies', 2, function () {
    var
      nextExpectedLog,
      console = {
        log: function (msg) {
          nextExpectedLog || (nextExpectedLog = 'user modified by view1');
          strictEqual(msg, nextExpectedLog, 'logged "' + msg + '"');
          nextExpectedLog = 'user modified by view2';
        }
      },
      SomeView = DummyView.extend({
        modifyModel: function () {
          this.model.set({ name: 'Betty' });
        }
      }),
      SomeOtherView = DummyView.extend({
        modifyModel: function () {
{'fraction_non_alphanumeric': 0.13006430868167201, 'fraction_numerical': 0.0022508038585209, 'mean_word_length': 2.4408185840707963, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2604140', 'n_tokens_mistral': 1827, 'n_tokens_neox': 1754, 'n_words': 569}