2015-04-29 01:50:09 -06:00
|
|
|
# Beautify = require '../src/beautify'
|
|
|
|
Beautifiers = require "../src/beautifiers"
|
|
|
|
beautifier = new Beautifiers()
|
2014-12-27 23:29:03 -07:00
|
|
|
fs = require "fs"
|
|
|
|
path = require "path"
|
2015-04-29 13:41:57 -06:00
|
|
|
JsDiff = require('diff')
|
2014-12-27 23:29:03 -07:00
|
|
|
|
|
|
|
# Use the command `window:run-package-specs` (cmd-alt-ctrl-p) to run specs.
|
|
|
|
#
|
|
|
|
# To run a specific `it` or `describe` block add an `f` to the front (e.g. `fit`
|
|
|
|
# or `fdescribe`). Remove the `f` to unfocus the block.
|
|
|
|
|
|
|
|
describe "BeautifyLanguages", ->
|
|
|
|
|
|
|
|
optionsDir = path.resolve(__dirname, "../examples")
|
|
|
|
|
|
|
|
# Activate all of the languages
|
|
|
|
allLanguages = [
|
|
|
|
"c", "coffee-script", "css", "html",
|
|
|
|
"java", "javascript", "json", "less",
|
|
|
|
"mustache", "objective-c", "perl", "php",
|
|
|
|
"python", "ruby", "sass", "sql",
|
2015-06-06 13:05:41 -06:00
|
|
|
"xml", "csharp", "gfm", "marko",
|
2015-05-18 15:08:06 -06:00
|
|
|
"tss", "go", "html-swig"
|
2014-12-27 23:29:03 -07:00
|
|
|
]
|
2015-06-06 13:05:41 -06:00
|
|
|
# All Atom packages that Atom Beautify is dependent on
|
|
|
|
dependentPackages = [
|
2015-06-06 13:56:01 -06:00
|
|
|
'autocomplete-plus'
|
|
|
|
'linter'
|
2015-06-10 13:45:34 -06:00
|
|
|
# 'atom-typescript' # it logs too much...
|
2015-06-06 13:05:41 -06:00
|
|
|
]
|
|
|
|
# Add language packages to dependentPackages
|
|
|
|
for lang in allLanguages
|
|
|
|
do (lang) ->
|
2015-06-06 13:56:01 -06:00
|
|
|
dependentPackages.push("language-#{lang}")
|
2014-12-27 23:29:03 -07:00
|
|
|
|
|
|
|
beforeEach ->
|
2015-01-02 22:58:52 -07:00
|
|
|
# Install all of the languages
|
2015-06-06 13:05:41 -06:00
|
|
|
for packageName in dependentPackages
|
|
|
|
do (packageName) ->
|
2014-12-27 23:29:03 -07:00
|
|
|
waitsForPromise ->
|
2015-06-06 13:05:41 -06:00
|
|
|
atom.packages.activatePackage(packageName)
|
2014-12-27 23:29:03 -07:00
|
|
|
|
2015-01-02 22:58:52 -07:00
|
|
|
# Activate package
|
|
|
|
waitsForPromise ->
|
|
|
|
activationPromise = atom.packages.activatePackage('atom-beautify')
|
|
|
|
# Force activate package
|
|
|
|
pack = atom.packages.getLoadedPackage("atom-beautify")
|
|
|
|
pack.activateNow()
|
2015-06-08 08:24:32 -06:00
|
|
|
# Check if Windows
|
|
|
|
isWindows = process.platform is 'win32' or
|
|
|
|
process.env.OSTYPE is 'cygwin' or
|
|
|
|
process.env.OSTYPE is 'msys'
|
|
|
|
# Need more debugging on Windows
|
|
|
|
if isWindows
|
|
|
|
# Change logger level
|
|
|
|
atom.config.set('atom-beautify._loggerLevel', 'verbose')
|
2015-01-02 22:58:52 -07:00
|
|
|
# Return promise
|
|
|
|
return activationPromise
|
|
|
|
|
|
|
|
# Set Uncrustify config path
|
|
|
|
# uncrustifyConfigPath = path.resolve(__dirname, "../examples/nested-jsbeautifyrc/uncrustify.cfg")
|
|
|
|
# uncrustifyLangs = ["c", "cpp", "objectivec", "cs", "d", "java", "pawn", "vala"]
|
|
|
|
# for lang in uncrustifyLangs
|
|
|
|
# do (lang) ->
|
|
|
|
# atom.config.set("atom-beautify.#{lang}_configPath", uncrustifyConfigPath)
|
|
|
|
# expect(atom.config.get("atom-beautify.#{lang}_configPath")).toEqual("TEST")
|
|
|
|
|
2014-12-27 23:29:03 -07:00
|
|
|
###
|
|
|
|
Directory structure:
|
2014-12-28 00:23:05 -07:00
|
|
|
- examples
|
|
|
|
- config1
|
|
|
|
- lang1
|
|
|
|
- original
|
|
|
|
- 1 - test.ext
|
|
|
|
- expected
|
|
|
|
- 1 - test.ext
|
|
|
|
- lang2
|
|
|
|
- config2
|
2014-12-27 23:29:03 -07:00
|
|
|
###
|
|
|
|
|
|
|
|
# All Configurations
|
|
|
|
configs = fs.readdirSync(optionsDir)
|
|
|
|
for config in configs
|
|
|
|
do (config) ->
|
|
|
|
# Generate the path to where all of the languages are
|
|
|
|
langsDir = path.resolve(optionsDir, config)
|
|
|
|
optionStats = fs.lstatSync(langsDir)
|
|
|
|
# Confirm that this path is a directory
|
|
|
|
if optionStats.isDirectory()
|
|
|
|
# Create testing group for configuration
|
|
|
|
describe "when using configuration '#{config}'", ->
|
|
|
|
# All Languages for configuration
|
|
|
|
langNames = fs.readdirSync(langsDir)
|
|
|
|
for lang in langNames
|
|
|
|
do (lang) ->
|
|
|
|
# Generate the path to where al of the tests are
|
|
|
|
testsDir = path.resolve(langsDir, lang)
|
|
|
|
langStats = fs.lstatSync(testsDir)
|
|
|
|
# Confirm that this path is a directory
|
|
|
|
if langStats.isDirectory()
|
|
|
|
# Original
|
|
|
|
originalDir = path.resolve(testsDir, "original")
|
|
|
|
if not fs.existsSync(originalDir)
|
|
|
|
console.warn("Directory for test originals/inputs not found." +
|
|
|
|
" Making it at #{originalDir}.")
|
|
|
|
fs.mkdirSync(originalDir)
|
|
|
|
# Expected
|
|
|
|
expectedDir = path.resolve(testsDir, "expected")
|
|
|
|
if not fs.existsSync(expectedDir)
|
|
|
|
console.warn("Directory for test expected/results not found." +
|
|
|
|
"Making it at #{expectedDir}.")
|
|
|
|
fs.mkdirSync(expectedDir)
|
|
|
|
|
|
|
|
# Language group tests
|
|
|
|
describe "when beautifying language '#{lang}'", ->
|
|
|
|
|
|
|
|
# All tests for language
|
|
|
|
testNames = fs.readdirSync(originalDir)
|
|
|
|
for testFileName in testNames
|
|
|
|
do (testFileName) ->
|
|
|
|
ext = path.extname(testFileName)
|
|
|
|
testName = path.basename(testFileName, ext)
|
2014-12-28 00:23:05 -07:00
|
|
|
# If prefixed with underscore (_) then this is a hidden test
|
|
|
|
if testFileName[0] is '_'
|
|
|
|
# Do not show this test
|
|
|
|
return
|
2014-12-27 23:29:03 -07:00
|
|
|
# Confirm this is a test
|
2014-12-28 00:23:05 -07:00
|
|
|
it "#{testName} #{testFileName}", ->
|
2014-12-27 23:29:03 -07:00
|
|
|
|
|
|
|
# Generate paths to test files
|
|
|
|
originalTestPath = path.resolve(originalDir, testFileName)
|
|
|
|
expectedTestPath = path.resolve(expectedDir, testFileName)
|
|
|
|
# Get contents of original test file
|
|
|
|
originalContents = fs.readFileSync(originalTestPath)?.toString()
|
|
|
|
# Check if there is a matching expected test resut
|
|
|
|
if not fs.existsSync(expectedTestPath)
|
|
|
|
throw new Error("No matching expected test result found for '#{testName}' " +
|
2014-12-28 00:23:05 -07:00
|
|
|
"at '#{expectedTestPath}'.")
|
2014-12-27 23:29:03 -07:00
|
|
|
# err = fs.writeFileSync(expectedTestPath, originalContents)
|
|
|
|
# throw err if err
|
|
|
|
# Get contents of expected test file
|
|
|
|
expectedContents = fs.readFileSync(expectedTestPath)?.toString()
|
|
|
|
# expect(expectedContents).not.toEqual originalContents
|
|
|
|
# expect(atom.grammars.getGrammars()).toEqual []
|
|
|
|
grammar = atom.grammars.selectGrammar(originalTestPath, originalContents)
|
|
|
|
# expect(grammar).toEqual("test")
|
|
|
|
grammarName = grammar.name
|
|
|
|
|
2014-12-28 13:18:30 -07:00
|
|
|
# Get the options
|
2015-04-29 01:50:09 -06:00
|
|
|
allOptions = beautifier.getOptionsForPath(originalTestPath)
|
2014-12-27 23:29:03 -07:00
|
|
|
|
|
|
|
beautifyCompleted = false
|
|
|
|
completionFun = (text) ->
|
2015-06-10 13:12:19 -06:00
|
|
|
# Replace Newlines
|
|
|
|
text = text.replace(/(?:\r\n|\r|\n)/g, '⏎\n')
|
|
|
|
expectedContents = expectedContents\
|
|
|
|
.replace(/(?:\r\n|\r|\n)/g, '⏎\n')
|
|
|
|
# Replace tabs
|
|
|
|
text = text.replace(/(?:\t)/g, '↹')
|
|
|
|
expectedContents = expectedContents\
|
|
|
|
.replace(/(?:\t)/g, '↹')
|
|
|
|
# Replace spaces
|
|
|
|
text = text.replace(/(?:\ )/g, '␣')
|
|
|
|
expectedContents = expectedContents\
|
|
|
|
.replace(/(?:\ )/g, '␣')
|
2015-05-19 12:02:23 -06:00
|
|
|
# logger.verbose(expectedTestPath, text) if ext is ".less"
|
2015-05-03 09:50:45 -06:00
|
|
|
expect(text instanceof Error).not.toEqual(true, text)
|
2015-05-21 13:43:37 -06:00
|
|
|
return beautifyCompleted = true if text instanceof Error
|
2015-05-03 09:50:45 -06:00
|
|
|
# if text instanceof Error
|
|
|
|
# return beautifyCompleted = text # text == Error
|
2015-05-21 13:43:37 -06:00
|
|
|
|
|
|
|
expect(text).not.toEqual(null, "Language or Beautifier not found")
|
|
|
|
return beautifyCompleted = true if text is null
|
|
|
|
|
|
|
|
expect(typeof text).toEqual("string", "Text: #{text}")
|
2015-06-10 10:11:20 -06:00
|
|
|
return beautifyCompleted = true if typeof text isnt "string"
|
2015-04-29 13:41:57 -06:00
|
|
|
# Check for beautification errors
|
2014-12-28 00:23:05 -07:00
|
|
|
if text isnt expectedContents
|
2015-04-29 13:41:57 -06:00
|
|
|
# console.warn(allOptions, text, expectedContents)
|
2015-04-30 10:23:41 -06:00
|
|
|
fileName = expectedTestPath
|
2015-04-29 13:41:57 -06:00
|
|
|
oldStr=text
|
|
|
|
newStr=expectedContents
|
2015-04-30 10:23:41 -06:00
|
|
|
oldHeader="beautified"
|
2015-04-29 13:41:57 -06:00
|
|
|
newHeader="expected"
|
|
|
|
diff = JsDiff.createPatch(fileName, oldStr, newStr, oldHeader, newHeader)
|
2015-05-03 09:20:31 -06:00
|
|
|
expect(text).toEqual(expectedContents, "Beautifier output does not match expected output:\n"+diff)
|
2015-04-29 13:41:57 -06:00
|
|
|
# All done!
|
2014-12-27 23:29:03 -07:00
|
|
|
beautifyCompleted = true
|
|
|
|
|
|
|
|
runs ->
|
|
|
|
try
|
2015-04-29 01:50:09 -06:00
|
|
|
beautifier.beautify(originalContents, allOptions, grammarName, testFileName)
|
|
|
|
.then(completionFun)
|
|
|
|
.catch(completionFun)
|
2014-12-27 23:29:03 -07:00
|
|
|
catch e
|
|
|
|
beautifyCompleted = e
|
|
|
|
|
|
|
|
waitsFor(->
|
|
|
|
if beautifyCompleted instanceof Error
|
|
|
|
throw beautifyCompleted
|
|
|
|
else
|
|
|
|
return beautifyCompleted
|
|
|
|
, "Waiting for beautification to complete", 5000)
|