File: conversion.js

package info (click to toggle)
node-yarnpkg 1.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 92,980 kB
  • sloc: sh: 323; makefile: 19
file content (43 lines) | stat: -rw-r--r-- 1,524 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/* @flow */

import {boolify, boolifyWithDefault} from '../../src/util/conversion.js';

describe('boolify', () => {
  test('should recognize "true" values', () => {
    expect(boolify('1')).toBe(true);
    expect(boolify(1)).toBe(true);
    expect(boolify('true')).toBe(true);
    expect(boolify(true)).toBe(true);
  });

  test('should recognize "false" values', () => {
    expect(boolify('0')).toBe(false);
    expect(boolify(0)).toBe(false);
    expect(boolify('false')).toBe(false);
    expect(boolify(false)).toBe(false);
  });

  // Not sure if that's really a requirement, but it's how it currently works
  test('should treat unrecognized values as "true"', () => {
    expect(boolify('randomstringdfkjh')).toBe(true);
  });
});

describe('boolifyWithDefault', () => {
  test('should return the default for empty values', () => {
    expect(boolifyWithDefault(undefined, true)).toBe(true);
    expect(boolifyWithDefault(undefined, false)).toBe(false);
    expect(boolifyWithDefault(null, true)).toBe(true);
    expect(boolifyWithDefault(null, false)).toBe(false);
    expect(boolifyWithDefault('', true)).toBe(true);
    expect(boolifyWithDefault('', false)).toBe(false);
  });

  test('should be consistent with boolify() for non-empty values', () => {
    for (const value of [1, '1', true, 'true', 0, '0', false, 'false', 'randomstringlkjsaljdja', 649815]) {
      for (const defaultResult of [true, false]) {
        expect(boolifyWithDefault(value, defaultResult)).toBe(boolify(value));
      }
    }
  });
});