Skip to content

Latest commit

 

History

History
55 lines (42 loc) · 1.69 KB

code-comment.spec.coffee.md

File metadata and controls

55 lines (42 loc) · 1.69 KB

Test Pattern Language is Commented

RULE: The first line of the block should be a comment of the language used inside it.

The convention I use when writting literate CoffeeScript is that code colorized as CoffeeScript is the actual code used to implement this rule, while example code does not have syntax highlighting.

Examples

    ```CoffeeScript
    # CoffeeScript
    ```
    ```JavaScript
    // JavaScript
    ```

This rule uses the isSupported and getCode functions from rule util.

    
    'use strict'
    util = require('../lib/rule.util')
    getCode = (lang) ->
        util.getCode "code-comment/#{lang}"

A simple method to create the expected comment based on the code language used in the block.

    expectedComment = (lang) ->
        throw new Error("Unknown language: #{lang}") unless util.isSupported(lang)
        switch lang
            when 'CoffeeScript'
                "# #{lang}"
            when 'JavaScript'
                "// #{lang}"

Define rule acceptance tests.

    describe 'the first line', ->
        it 'should be a comment with the code language name', ->
            #validate based on language
            block = getCode 'javascript'
            firstLine = block.contents.split('\n')[0]
            expect(firstLine).toBe(expectedComment block.lang)
            
            block = getCode 'coffeescript'
            firstLine = block.contents.split('\n')[0]
            expect(firstLine).toBe(expectedComment block.lang)