'use strict';

import {expect} from 'chai';
import YAWN from '../src/index.js';

describe('preserves comments and styling in objects when', ()=> {

  describe('JSON is an object and', ()=> {

    it('transformed to an array', ()=> {
      // debugger
      let str = `
        # leading comment
        value: 1 # inline comment
        number: 42
        # trailing comment`;

      let yawn = new YAWN(str);
      yawn.json = ['foo', 'bar', 'baz'];

      expect(yawn.yaml).to.equal(`
        # leading comment
        - foo
        - bar
        - baz
        # trailing comment`);
    });

    it('one of the values changes', ()=> {

      let str = `
        # leading comment
        value: 1 # inline comment
        number: 42
        # trailing comment`;

      let yawn = new YAWN(str);
      let json = yawn.json;
      json.value = 2;
      yawn.json = json;

      expect(yawn.yaml).to.equal(`
        # leading comment
        value: 2 # inline comment
        number: 42
        # trailing comment`);
    });

    it('two values changes', ()=> {

      let str = `
        # leading comment
        value: 1 # inline comment
        number: 42
        # trailing comment`;

      let yawn = new YAWN(str);
      let json = yawn.json;
      json.value = 2;
      json.number = 200;
      yawn.json = json;

      expect(yawn.yaml).to.equal(`
        # leading comment
        value: 2 # inline comment
        number: 200
        # trailing comment`);
    });

    it('one of the values is deleted', ()=> {
      let str = `
        # leading comment
        value: 1 # inline comment
        number: 42
        # trailing comment`;

      let yawn = new YAWN(str);
      let json = yawn.json;
      delete json.value;
      yawn.json = json;

      expect(yawn.yaml).to.equal(`
        # leading comment
         # inline comment
        number: 42
        # trailing comment`);
    });

    it('a new primitive value has been added', ()=> {
      let str = `
        # leading comment
        value: 1 # inline comment
        number: 42
        # trailing comment`;

      let yawn = new YAWN(str);
      let json = yawn.json;
      json.newVal = 99;
      yawn.json = json;

      expect(yawn.yaml).to.equal(`
        # leading comment
        value: 1 # inline comment
        number: 42
        newVal: 99
        # trailing comment`);
    });

    it('a new non-primitive value has been added', ()=> {
      let str = `
        # leading comment
        value: 1 # inline comment
        number: 42
        # trailing comment`;

      let yawn = new YAWN(str);
      let json = yawn.json;
      json.newVal = {ans: 42, cond: 'good'};
      yawn.json = json;

      expect(yawn.yaml).to.equal(`
        # leading comment
        value: 1 # inline comment
        number: 42
        newVal:
          ans: 42
          cond: good
        # trailing comment`);
    });
  });
});

@@ -2879,16 +2879,1280 @@
 %0A    %7D);
+%0A%0A    it('two new non-primitive value has been added to a map', ()=%3E %7B%0A      let str = %60%0A        # leading comment%0A        map:%0A          val: 1%0A        # trailing comment%60;%0A%0A      let yawn = new YAWN(str);%0A      let json = yawn.json;%0A      json.map = %7B%0A        val: 1,%0A        hash1: %7Bans: 42, cond: 'good'%7D,%0A        hash2: %7Bans: 34, cond: 'not-bad'%7D%0A      %7D;%0A      yawn.json = json;%0A%0A      expect(yawn.yaml).to.equal(%60%0A        # leading comment%0A        map:%0A          val: 1%0A          hash2:%0A            ans: 34%0A            cond: not-bad%0A          hash1:%0A            ans: 42%0A            cond: good%0A        # trailing comment%60);%0A    %7D);%0A%0A    it('two new non-primitive value has been added to root', ()=%3E %7B%0A      let str = %60%0A        # leading comment%0A        map:%0A          val: 1%0A        # trailing comment%60;%0A%0A      let yawn = new YAWN(str);%0A      let json = yawn.json;%0A      json = %7B%0A        map: %7Bval: 1%7D,%0A        hash1: %7Bans: 42, cond: 'good'%7D,%0A        hash2: %7Bans: 34, cond: 'not-bad'%7D%0A      %7D;%0A      yawn.json = json;%0A%0A      expect(yawn.yaml).to.equal(%60%0A        # leading comment%0A        map:%0A          val: 1%0A        hash2:%0A          ans: 34%0A          cond: not-bad%0A        hash1:%0A          ans: 42%0A          cond: good%0A        # trailing comment%60);%0A    %7D);
 %0A  %7D);%0A%7D

Add more test to object