/*
 * Copyright (C) Sapphirecode - All Rights Reserved
 * This file is part of console-app which is released under MIT.
 * See file 'LICENSE' for full license details.
 * Created by Timo Hocker <timo@scode.ovh>, May 2020
 */

import test from 'ava';
import { TypeValidation } from '../lib/TypeValidation/TypeValidation';

test ('string', async (t) => {
  const validator = new TypeValidation ('string');
  const res = await validator.to_type ('foo');
  t.is (res, 'foo');
});

test ('no number', (t) => {
  const validator = new TypeValidation ('number');
  t.throws (
    () => validator.to_type ('foo'),
    { message: 'value is not a number' }
  );
});

test ('number', async (t) => {
  const validator = new TypeValidation ('number');
  const res = await validator.to_type ('123.4');
  t.is (res, 123.4);
});

test ('int', async (t) => {
  const validator = new TypeValidation ('int');
  const res = await validator.to_type ('123.4');
  t.is (res, 123);
});

test ('no boolean', (t) => {
  const validator = new TypeValidation ('boolean');
  t.throws (
    () => validator.to_type ('foo'),
    { message: 'value is not a boolean' }
  );
});

test ('boolean', async (t) => {
  const validator = new TypeValidation ('boolean');
  const r1 = await validator.to_type ('false');
  const r2 = await validator.to_type ('true');
  t.is (r1, false);
  t.is (r2, true);
});

test ('boolean number', async (t) => {
  const validator = new TypeValidation ('boolean');
  const r1 = await validator.to_type (0);
  const r2 = await validator.to_type (1);
  t.is (r1, false);
  t.is (r2, true);
});

test ('no array', (t) => {
  const validator = new TypeValidation ('array');
  t.throws (
    () => validator.to_type (1),
    { message: 'value is not an array' }
  );
});

test ('array', async (t) => {
  const validator = new TypeValidation ('array');
  const res = await validator.to_type ([
    'foo',
    'bar',
    'baz'
  ]);
  t.deepEqual (res, [
    'foo',
    'bar',
    'baz'
  ]);
});

test ('string array', async (t) => {
  const validator = new TypeValidation ('array');
  const res = await validator.to_type ('f o o,bar  ,  baz');
  t.deepEqual (res, [
    'f o o',
    'bar',
    'baz'
  ]);
});