<commit_before>import MenuView from '../../../src/view/menu/MenuView'
const assert = require('assert')
const sinon = require('sinon')

describe('MenuView tests', () =>{
  var leftBorderCoordinate = 0
  var leftPadding = 2
  var sectionPadding = 5
  var linePadding = 3
  var buttonWidth = 1
  var buttonHeight = 4
  var fontSize = 10
  var groupStub
  var gameStub
  var menuView
  var tile

  beforeEach(() =>{
    var stub = sinon.stub()
    groupStub = {add: function(){}, create: function(){}, removeAll: function(){}}
    stub.withArgs().returns(groupStub)
    gameStub = {add: {group: stub}, make: {}}

    menuView = new MenuView({
      game: gameStub,
      leftBorderCoordinate: leftBorderCoordinate,
      leftPadding: leftPadding,
      sectionPadding: sectionPadding,
      linePadding: linePadding,
      buttonWidth: buttonWidth,
      buttonHeight: buttonHeight,
      fontSize: fontSize
    })

    menuView.drawHeight = 0
    tile = {tileType: {name: 'test'}, x: 0, y: 0, structure: {structureType: {name: 'test2'}}}
    menuView.menu = {selectedTile: tile}
  })

  it('Constructor works', () =>{
    assert.equal(gameStub, menuView.game)
    assert.equal(leftBorderCoordinate, menuView.leftBorderCoordinate)
    assert.equal(leftPadding, menuView.leftPadding)
    assert.equal(sectionPadding, menuView.sectionPadding)
    assert.equal(linePadding, menuView.linePadding)
    assert.equal(buttonWidth, menuView.buttonWidth)
    assert.equal(buttonHeight, menuView.buttonHeight)
    assert.equal(groupStub, menuView.menuViewGroup)
    assert.equal(groupStub.fixedToCamera, true)
    assert.equal(0, menuView.buttonActions.length)
  })

  it('Set menu works', () =>{
    menuView.setMenu(12)
    assert.equal(12, menuView.menu)
  })

  it('Padding adders work', () =>{
    menuView.drawHeight = 0
    menuView.addPadding(10)
    assert.equal(10, menuView.drawHeight)

    menuView.drawHeight = 0
    menuView.addLinePadding()
    assert.equal(linePadding, menuView.drawHeight)

    menuView.drawHeight = 0
    menuView.addSectionPadding()
    assert.equal(sectionPadding, menuView.drawHeight)
  })

  it('Setting button actions works', () =>{
    var spy = sinon.spy()
    menuView.redraw = spy

    menuView.setButtonActions([2,3])

    assert.equal(2, menuView.buttonActions[0])
    assert.equal(3, menuView.buttonActions[1])
    assert.equal(1, spy.callCount)

    menuView.setButtonActions([])
    assert.equal(0, menuView.buttonActions.length)
    assert.equal(2, spy.callCount)
  })

  it('Texts are created correctly', () =>{
    var textSpy = sinon.spy()
    gameStub.add.text = textSpy
    var paddingSpy = sinon.spy()
    menuView.addPadding = paddingSpy

    menuView.createText("asd", 10)

    var style = {font: fontSize+"px Arial", fill: "#ffff00", align: "center"}
    assert.equal(1, textSpy.callCount)
    assert(textSpy.calledWith(leftBorderCoordinate+leftPadding, 0, "asd", style, groupStub))
    assert(paddingSpy.calledWith(10))
  })

  it('Button is created correctly', () =>{
    var anchorSpy = sinon.spy()
    var textSpy = sinon.stub()
    gameStub.add.text = textSpy
    textSpy.returns({anchor: {set: anchorSpy}})
    var buttonSpy = sinon.spy()
    gameStub.make.button = buttonSpy
    var paddingSpy = sinon.spy()
    menuView.addPadding = paddingSpy

    menuView.createButton({name: "asd", function: function(){}, context: this})

    assert.equal(1, textSpy.callCount)
    assert.equal(1, buttonSpy.callCount)
    assert(paddingSpy.calledWith(buttonHeight))
    assert.equal(1, anchorSpy.callCount)
  })

  it('Buttons are created correctly', () =>{
    var paddingSpy = sinon.spy()
    menuView.addLinePadding = paddingSpy
    var buttonSpy = sinon.spy()
    menuView.createButton = buttonSpy

    assert(menuView.createButtons())
    assert.equal(0, paddingSpy.callCount)
    assert.equal(0, buttonSpy.callCount)

    menuView.buttonActions = [1,2,3,4]

    assert(menuView.createButtons())
    assert.equal(4, paddingSpy.callCount)
    for(var i = 0 ; i < menuView.buttonActions.length ; i++){
      assert(buttonSpy.calledWith(menuView.buttonActions[i]))
    }
  })

  it('Tile information is created correctly', () =>{
    var paddingSpy = sinon.spy()
    menuView.addLinePadding = paddingSpy
    var textSpy = sinon.spy()
    menuView.createText = textSpy

    assert(menuView.createTileInformation())

    assert.equal(1, paddingSpy.callCount)
    assert(textSpy.calledWith('Ground type: test', 16))
    assert(textSpy.calledWith('X: 0, Y: 0', 16))
  })

  it('Structure information isn\'t created when structure isn\'t found', () =>{
    var paddingSpy = sinon.spy()
    menuView.addLinePadding = paddingSpy
    var textSpy = sinon.spy()
    menuView.createText = textSpy

    tile.structure = null

    assert(!menuView.createStructureInformation())

    assert.equal(0, paddingSpy.callCount)
    assert.equal(0, textSpy.callCount)
  })

  it('Structure infromation is created when structure is found', () =>{
    var paddingSpy = sinon.spy()
    menuView.addLinePadding = paddingSpy
    var textSpy = sinon.spy()
    menuView.createText = textSpy

    assert(menuView.createStructureInformation())

    assert.equal(0, paddingSpy.callCount)
    assert(textSpy.calledWith('Structure: test2', 16))
  })

  it('Background creation works', () =>{
    var spy = sinon.spy()
    groupStub.create = spy<commit_msg>menu view testejä yksi jäljellä<commit_after>import MenuView from '../../../src/view/menu/MenuView'
const assert = require('assert')
const sinon = require('sinon')

describe('MenuView tests', () =>{
  var leftBorderCoordinate = 0
  var leftPadding = 2
  var sectionPadding = 5
  var linePadding = 3
  var buttonWidth = 1
  var buttonHeight = 4
  var fontSize = 10
  var groupStub
  var gameStub
  var menuView
  var tile

  beforeEach(() =>{
    var stub = sinon.stub()
    groupStub = {add: function(){}, create: function(){}, removeAll: function(){}}
    stub.withArgs().returns(groupStub)
    gameStub = {add: {group: stub}, make: {}}

    menuView = new MenuView({
      game: gameStub,
      leftBorderCoordinate: leftBorderCoordinate,
      leftPadding: leftPadding,
      sectionPadding: sectionPadding,
      linePadding: linePadding,
      buttonWidth: buttonWidth,
      buttonHeight: buttonHeight,
      fontSize: fontSize
    })

    menuView.drawHeight = 0
    tile = {tileType: {name: 'test'}, x: 0, y: 0, structure: {structureType: {name: 'test2'}}}
    menuView.menu = {selectedTile: tile}
  })

  it('Constructor works', () =>{
    assert.equal(gameStub, menuView.game)
    assert.equal(leftBorderCoordinate, menuView.leftBorderCoordinate)
    assert.equal(leftPadding, menuView.leftPadding)
    assert.equal(sectionPadding, menuView.sectionPadding)
    assert.equal(linePadding, menuView.linePadding)
    assert.equal(buttonWidth, menuView.buttonWidth)
    assert.equal(buttonHeight, menuView.buttonHeight)
    assert.equal(groupStub, menuView.menuViewGroup)
    assert.equal(groupStub.fixedToCamera, true)
    assert.equal(0, menuView.buttonActions.length)
  })

  it('Set menu works', () =>{
    menuView.setMenu(12)
    assert.equal(12, menuView.menu)
  })

  it('Padding adders work', () =>{
    menuView.drawHeight = 0
    menuView.addPadding(10)
    assert.equal(10, menuView.drawHeight)

    menuView.drawHeight = 0
    menuView.addLinePadding()
    assert.equal(linePadding, menuView.drawHeight)

    menuView.drawHeight = 0
    menuView.addSectionPadding()
    assert.equal(sectionPadding, menuView.drawHeight)
  })

  it('Setting button actions works', () =>{
    var spy = sinon.spy()
    menuView.redraw = spy

    menuView.setButtonActions([2,3])

    assert.equal(2, menuView.buttonActions[0])
    assert.equal(3, menuView.buttonActions[1])
    assert.equal(1, spy.callCount)

    menuView.setButtonActions([])
    assert.equal(0, menuView.buttonActions.length)
    assert.equal(2, spy.callCount)
  })

  it('Texts are created correctly', () =>{
    var textSpy = sinon.spy()
    gameStub.add.text = textSpy
    var paddingSpy = sinon.spy()
    menuView.addPadding = paddingSpy

    menuView.createText("asd", 10)

    var style = {font: fontSize+"px Arial", fill: "#ffff00", align: "center"}
    assert.equal(1, textSpy.callCount)
    assert(textSpy.calledWith(leftBorderCoordinate+leftPadding, 0, "asd", style, groupStub))
    assert(paddingSpy.calledWith(10))
  })

  it('Button is created correctly', () =>{
    var anchorSpy = sinon.spy()
    var textSpy = sinon.stub()
    gameStub.add.text = textSpy
    textSpy.returns({anchor: {set: anchorSpy}})
    var buttonSpy = sinon.spy()
    gameStub.make.button = buttonSpy
    var paddingSpy = sinon.spy()
    menuView.addPadding = paddingSpy

    menuView.createButton({name: "asd", function: function(){}, context: this})

    assert.equal(1, textSpy.callCount)
    assert.equal(1, buttonSpy.callCount)
    assert(paddingSpy.calledWith(buttonHeight))
    assert.equal(1, anchorSpy.callCount)
  })

  it('Buttons are created correctly', () =>{
    var paddingSpy = sinon.spy()
    menuView.addLinePadding = paddingSpy
    var buttonSpy = sinon.spy()
    menuView.createButton = buttonSpy

    assert(menuView.createButtons())
    assert.equal(0, paddingSpy.callCount)
    assert.equal(0, buttonSpy.callCount)

    menuView.buttonActions = [1,2,3,4]

    assert(menuView.createButtons())
    assert.equal(4, paddingSpy.callCount)
    for(var i = 0 ; i < menuView.buttonActions.length ; i++){
      assert(buttonSpy.calledWith(menuView.buttonActions[i]))
    }
  })

  it('Tile information is created correctly', () =>{
    var paddingSpy = sinon.spy()
    menuView.addLinePadding = paddingSpy
    var textSpy = sinon.spy()
    menuView.createText = textSpy

    assert(menuView.createTileInformation())

    assert.equal(1, paddingSpy.callCount)
    assert(textSpy.calledWith('Ground type: test'))
    assert(textSpy.calledWith('X: 0, Y: 0'))
  })

  it('Structure information isn\'t created when structure isn\'t found', () =>{
    var paddingSpy = sinon.spy()
    menuView.addLinePadding = paddingSpy
    var textSpy = sinon.spy()
    menuView.createText = textSpy

    tile.structure = null

    assert(!menuView.createStructureInformation())

    assert.equal(0, paddingSpy.callCount)
    assert.equal(0, textSpy.callCount)
  })

  it('Structure infromation is created when structure is found', () =>{
    var paddingSpy = sinon.spy()
    menuView.addLinePadding = paddingSpy
    var textSpy = sinon.spy()
    menuView.createText = textSpy

    assert(menuView.createStructureInformation())

    assert.equal(0, paddingSpy.callCount)
    assert(textSpy.calledWith('Structure: test2', 16))
  })

  it('Background creation works', () =>{
    var spy = sinon.spy()
    groupStub.create = spy
{'fraction_non_alphanumeric': 0.13016759776536313, 'fraction_numerical': 0.011731843575418994, 'mean_word_length': 3.7296345222369, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, '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': '3079026', 'n_tokens_mistral': 3589, 'n_tokens_neox': 3501, 'n_words': 695}