<commit_before>fs      = require 'fs'
{inspect} = require 'util'
walkdir = require 'walkdir'
Parser  = require '../src/parser'
Referencer = require '../src/util/referencer'
Generator = require '../src/generator'

{diff}    = require 'jsondiffpatch'
_         = require 'underscore'
_.str     = require 'underscore.string'
require('jasmine-focused')

describe "Parser", ->
  parser = null

  constructDelta = (filename, hasReferences = false) ->
    source = fs.readFileSync filename, 'utf8'

    parser.parseContent source, filename

    expected = JSON.stringify(JSON.parse(fs.readFileSync filename.replace(/\.coffee$/, '.json'), 'utf8'), null, 2)
    generated = if hasReferences then followReferences(parser) else JSON.stringify(parser.toJSON(), null, 2)

    diff(expected, generated)

  followReferences = (parser) ->
    parser.parseFile "spec/templates/methods/method_example.coffee"
    parser.parseFile "spec/templates/methods/curly_method_documentation.coffee"
    parser.parseFile "spec/templates/methods/fixtures/private_class.coffee"

    # since delegation happens in the generator, we need to force that magic here
    generator = new Generator(parser,
                              noOutput: true
                              stats: true
                              extras: []
                              quiet: false
                            )
    referencer = new Referencer(parser.classes, parser.mixins, {quiet: false})
    for clazz in parser.classes
      methods = clazz.getMethods()

      # resolve all delegations in methods
      for method in methods
        delegation = method.doc.delegation
        if delegation
          originalStatus = method.doc.status
          [method.doc, method.parameters] = referencer.resolveDelegation(method, delegation, clazz)
          method.doc.status = originalStatus

    # [0], because we don't want the parsed files in the resulting JSON
    JSON.stringify([parser.toJSON()[0]], null, 2)

  checkDelta = (delta) ->
    if delta?
      console.error(inspect(delta))
      expect(delta).toBe(undefined)

  beforeEach ->
    parser = new Parser({
      inputs: []
      output: ''
      extras: []
      readme: ''
      title: ''
      quiet: false
      private: true
      github: ''
    })

  describe "Classes", ->
    it 'understands descriptions', ->
      delta = constructDelta("spec/templates/classes/class_description_markdown.coffee")
      checkDelta(delta)

    it 'understands documentation', ->
      delta = constructDelta("spec/templates/classes/class_documentation.coffee")
      checkDelta(delta)

    it 'understands extends', ->
      delta = constructDelta("spec/templates/classes/class_extends.coffee")
      checkDelta(delta)

    it 'understands empty classes', ->
      delta = constructDelta("spec/templates/classes/empty_class.coffee")
      checkDelta(delta)

    it 'understands exporting classess', ->
      delta = constructDelta("spec/templates/classes/export_class.coffee")
      checkDelta(delta)

    it 'understands inner classes', ->
      delta = constructDelta("spec/templates/classes/inner_class.coffee")
      checkDelta(delta)

    it 'understands namespaced classes', ->
      delta = constructDelta("spec/templates/classes/namespaced_class.coffee")
      checkDelta(delta)

    it 'understands simple classes', ->
      delta = constructDelta("spec/templates/classes/simple_class.coffee")
      checkDelta(delta)

  describe "non class files", ->
    it 'understands descriptions', ->
      delta = constructDelta("spec/templates/files/non_class_file.coffee")
      checkDelta(delta)

  describe "Methods", ->
    it 'understands assigned parameters classes', ->
      delta = constructDelta("spec/templates/methods/assigned_parameters.coffee")
      checkDelta(delta)

    it 'understands class methods', ->
      delta = constructDelta("spec/templates/methods/class_methods.coffee")
      checkDelta(delta)

    it 'understands curly notation', ->
      delta = constructDelta("spec/templates/methods/curly_method_documentation.coffee")
      checkDelta(delta)

    it 'understands hash parameters', ->
      delta = constructDelta("spec/templates/methods/hash_parameters.coffee")
      checkDelta(delta)

    it 'understands instance methods', ->
      delta = constructDelta("spec/templates/methods/instance_methods.coffee")
      checkDelta(delta)

    it 'understands links in methods', ->
      delta = constructDelta("spec/templates/methods/links.coffee")
      checkDelta(delta)

    it 'understands method delegation', ->
      delta = constructDelta("spec/templates/methods/method_delegation.coffee", true)
      checkDelta(delta)

    it 'understands method delegation from public to private', ->
      delta = constructDelta("spec/templates/methods/method_delegation_as_private.coffee", true)
      checkDelta(delta)

    it 'understands basic methods', ->
      delta = constructDelta("spec/templates/methods/method_example.coffee")
      checkDelta(delta)

    it 'understands methods with paragraph descriptions for parameters', ->
      delta = constructDelta("spec/templates/methods/method_paragraph_param.coffee")
      checkDelta(delta)

    it 'understands methods with no descriptions', ->
      delta = constructDelta("spec/templates/methods/method_shortdesc.coffee")
      checkDelta(delta)

    it 'understands optional arguments', ->
      delta = constructDelta("spec/templates/methods/optional_arguments.coffee")
      checkDelta(delta)

    it 'understands paragraph length descriptions', ->
      delta = constructDelta("spec/templates/methods/paragraph_desc.coffee")
      checkDelta(delta)

    it 'understands preprocessor flagging for visibility', ->
      delta = constructDelta("spec/templates/methods/preprocessor_flagging.coffee")
      checkDelta(delta)

    fit 'understands prototypical methods', ->
      delta = constructDelta("spec/templates/methods/prototypical_methods.coffee")
      checkDelta(delta)

    it 'understands return values', ->
      delta = constructDelta("spec/templates/methods/return_values.coffee")
      checkDelta(delta)

    it 'understands paragraph length return values', ->
      delta = constructDelta("spec/templates/methods/return_values_long.coffee")
      checkDelta(delta)
<commit_msg>Simplify test<commit_after>fs      = require 'fs'
{inspect} = require 'util'
walkdir = require 'walkdir'
Parser  = require '../src/parser'
Referencer = require '../src/util/referencer'
Generator = require '../src/generator'

{diff}    = require 'jsondiffpatch'
_         = require 'underscore'
_.str     = require 'underscore.string'
require('jasmine-focused')

describe "Parser", ->
  parser = null

  constructDelta = (filename, hasReferences = false) ->
    source = fs.readFileSync filename, 'utf8'

    parser.parseContent source, filename

    expected = JSON.stringify(JSON.parse(fs.readFileSync filename.replace(/\.coffee$/, '.json'), 'utf8'), null, 2)
    generated = if hasReferences then followReferences(parser) else JSON.stringify(parser.toJSON(), null, 2)

    diff(expected, generated)
    checkDelta(expected, generated, diff(expected, generated))

  followReferences = (parser) ->
    parser.parseFile "spec/templates/methods/method_example.coffee"
    parser.parseFile "spec/templates/methods/curly_method_documentation.coffee"
    parser.parseFile "spec/templates/methods/fixtures/private_class.coffee"

    # since delegation happens in the generator, we need to force that magic here
    generator = new Generator(parser,
                              noOutput: true
                              stats: true
                              extras: []
                              quiet: false
                            )
    referencer = new Referencer(parser.classes, parser.mixins, {quiet: false})
    for clazz in parser.classes
      methods = clazz.getMethods()

      # resolve all delegations in methods
      for method in methods
        delegation = method.doc.delegation
        if delegation
          originalStatus = method.doc.status
          [method.doc, method.parameters] = referencer.resolveDelegation(method, delegation, clazz)
          method.doc.status = originalStatus

    # [0], because we don't want the parsed files in the resulting JSON
    JSON.stringify([parser.toJSON()[0]], null, 2)

  checkDelta = (expected, generated, delta) ->
    if delta?
      console.error expected, generated
      console.error(delta)
      expect(delta).toBe(undefined)

  beforeEach ->
    parser = new Parser({
      inputs: []
      output: ''
      extras: []
      readme: ''
      title: ''
      quiet: false
      private: true
      verbose: true
      github: ''
    })

  describe "Classes", ->
    it 'understands descriptions', ->
      constructDelta("spec/templates/classes/class_description_markdown.coffee")

    it 'understands documentation', ->
      constructDelta("spec/templates/classes/class_documentation.coffee")

    it 'understands extends', ->
      constructDelta("spec/templates/classes/class_extends.coffee")

    it 'understands empty classes', ->
      constructDelta("spec/templates/classes/empty_class.coffee")

    it 'understands exporting classess', ->
      constructDelta("spec/templates/classes/export_class.coffee")

    it 'understands inner classes', ->
      constructDelta("spec/templates/classes/inner_class.coffee")

    it 'understands namespaced classes', ->
      constructDelta("spec/templates/classes/namespaced_class.coffee")

    it 'understands simple classes', ->
      constructDelta("spec/templates/classes/simple_class.coffee")

  describe "non class files", ->
    it 'understands descriptions', ->
      constructDelta("spec/templates/files/non_class_file.coffee")

  describe "Methods", ->
    it 'understands assigned parameters classes', ->
      constructDelta("spec/templates/methods/assigned_parameters.coffee")

    it 'understands class methods', ->
      constructDelta("spec/templates/methods/class_methods.coffee")

    it 'understands curly notation', ->
      constructDelta("spec/templates/methods/curly_method_documentation.coffee")

    it 'understands hash parameters', ->
      constructDelta("spec/templates/methods/hash_parameters.coffee")

    it 'understands instance methods', ->
      constructDelta("spec/templates/methods/instance_methods.coffee")

    it 'understands links in methods', ->
      constructDelta("spec/templates/methods/links.coffee")

    it 'understands method delegation', ->
      constructDelta("spec/templates/methods/method_delegation.coffee", true)

    it 'understands method delegation from public to private', ->
      constructDelta("spec/templates/methods/method_delegation_as_private.coffee", true)

    it 'understands basic methods', ->
      constructDelta("spec/templates/methods/method_example.coffee")

    it 'understands methods with paragraph descriptions for parameters', ->
      constructDelta("spec/templates/methods/method_paragraph_param.coffee")

    it 'understands methods with no descriptions', ->
      constructDelta("spec/templates/methods/method_shortdesc.coffee")

    it 'understands optional arguments', ->
      constructDelta("spec/templates/methods/optional_arguments.coffee")

    it 'understands paragraph length descriptions', ->
      constructDelta("spec/templates/methods/paragraph_desc.coffee")

    it 'understands preprocessor flagging for visibility', ->
      constructDelta("spec/templates/methods/preprocessor_flagging.coffee")

    fit 'understands prototypical methods', ->
      constructDelta("spec/templates/methods/prototypical_methods.coffee")

    it 'understands return values', ->
      constructDelta("spec/templates/methods/return_values.coffee")

    it 'understands paragraph length return values', ->
      constructDelta("spec/templates/methods/return_values_long.coffee")

{'fraction_non_alphanumeric': 0.10904188084604365, 'fraction_numerical': 0.0011797421420746607, 'mean_word_length': 3.2629310344827585, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 71, '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': '1323858', 'n_tokens_mistral': 3332, 'n_tokens_neox': 3096, 'n_words': 690}