• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

source-academy / js-slang / 14529259416

18 Apr 2025 03:46AM UTC coverage: 80.538%. Remained the same
14529259416

Pull #1757

github

web-flow
Merge 63eac9783 into fea2b4cad
Pull Request #1757: Language options

3458 of 4687 branches covered (73.78%)

Branch coverage included in aggregate %.

15 of 88 new or added lines in 6 files covered. (17.05%)

84 existing lines in 6 files now uncovered.

10811 of 13030 relevant lines covered (82.97%)

142544.19 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

63.24
/src/repl/utils.ts
1
import { Option } from '@commander-js/extra-typings'
1✔
2

3
import { pyLanguages, scmLanguages, sourceLanguages } from '../constants'
1✔
4
import { Chapter, type Language, Variant, type Result, LanguageOptions } from '../types'
1✔
5
import { stringify } from '../utils/stringify'
1✔
6
import Closure from '../cse-machine/closure'
1✔
7
import { parseError, type Context } from '..'
1✔
8

9
export function chapterParser(str: string): Chapter {
1✔
10
  let foundChapter: string | undefined
11

12
  if (/^-?[0-9]+$/.test(str)) {
10✔
13
    // Chapter is fully numeric
14
    const value = parseInt(str)
5✔
15
    foundChapter = Object.keys(Chapter).find(chapterName => Chapter[chapterName] === value)
70✔
16

17
    if (foundChapter === undefined) {
5✔
18
      throw new Error(`Invalid chapter value: ${str}`)
1✔
19
    }
20
  } else {
21
    foundChapter = str
5✔
22
  }
23

24
  if (foundChapter in Chapter) {
9✔
25
    return Chapter[foundChapter]
8✔
26
  }
27
  throw new Error(`Invalid chapter value: ${str}`)
1✔
28
}
29

30
export const getChapterOption = <T extends Chapter>(
1✔
31
  defaultValue: T,
32
  argParser: (value: string) => T
33
) => {
34
  return new Option('--chapter <chapter>').default(defaultValue).argParser(argParser)
10✔
35
}
36

37
export const getVariantOption = <T extends Variant>(defaultValue: T, choices: T[]) => {
1✔
38
  return new Option('--variant <variant>').default(defaultValue).choices(choices)
10✔
39
}
40

41
export const getLanguageOption = <T extends LanguageOptions>() => {
1✔
42
  return new Option('--languageOptions <options>')
10✔
43
    .default({})
44
    .argParser((value: string): LanguageOptions => {
NEW
45
      const languageOptions = value.split(',').map(lang => {
×
NEW
46
        const [key, value] = lang.split('=')
×
NEW
47
        return { [key]: value }
×
48
      })
NEW
49
      return Object.assign({}, ...languageOptions)
×
50
    })
51
}
52

53
export function validateChapterAndVariantCombo(language: Language) {
1✔
UNCOV
54
  for (const { chapter, variant } of sourceLanguages) {
×
UNCOV
55
    if (language.chapter === chapter && language.variant === variant) return true
×
56
  }
57
  return false
×
58
}
59

60
/**
61
 * Returns true iff the given chapter and variant combination is supported.
62
 */
63
export function validChapterVariant(language: Language) {
1✔
64
  const { chapter, variant } = language
10✔
65

66
  for (const lang of sourceLanguages) {
10✔
67
    if (lang.chapter === chapter && lang.variant === variant) return true
90✔
68
  }
69
  for (const lang of scmLanguages) {
×
UNCOV
70
    if (lang.chapter === chapter && lang.variant === variant) return true
×
71
  }
UNCOV
72
  for (const lang of pyLanguages) {
×
UNCOV
73
    if (lang.chapter === chapter && lang.variant === variant) return true
×
74
  }
75

UNCOV
76
  return false
×
77
}
78

79
export function handleResult(result: Result, context: Context, verboseErrors: boolean) {
1✔
80
  if (result.status === 'finished') {
28✔
81
    if (result.representation !== undefined) {
23!
82
      return result.representation
×
83
    }
84

85
    if (result.value instanceof Closure || typeof result.value === 'function') {
23!
UNCOV
86
      return result.value.toString()
×
87
    }
88

89
    return stringify(result.value)
23✔
90
  }
91

92
  return `Error: ${parseError(context.errors, verboseErrors)}`
5✔
93
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc