unicode-bom 0.1.0

Unicode byte-order mark detection for files and byte arrays.
// Copyright © 2018 Phil Booth
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at:
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.

use super::*;

#[test]
fn as_ref() {
    assert_eq!(Bom::Null.as_ref(), "[not set]");
    assert_eq!(Bom::Bocu1.as_ref(), "BOCU-1");
    assert_eq!(Bom::Gb18030.as_ref(), "GB 18030");
    assert_eq!(Bom::Scsu.as_ref(), "SCSU");
    assert_eq!(Bom::UtfEbcdic.as_ref(), "UTF-EBCDIC");
    assert_eq!(Bom::Utf1.as_ref(), "UTF-1");
    assert_eq!(Bom::Utf7.as_ref(), "UTF-7");
    assert_eq!(Bom::Utf8.as_ref(), "UTF-8");
    assert_eq!(Bom::Utf16Be.as_ref(), "UTF-16 (big-endian)");
    assert_eq!(Bom::Utf16Le.as_ref(), "UTF-16 (little-endian)");
    assert_eq!(Bom::Utf32Be.as_ref(), "UTF-32 (big-endian)");
    assert_eq!(Bom::Utf32Le.as_ref(), "UTF-32 (little-endian)");
}

#[test]
fn to_string() {
    assert_eq!(Bom::Null.to_string(), "[not set]");
}

#[test]
fn default() {
    assert_eq!(Bom::default(), Bom::Null);
}

macro_rules! assert_slice {
    ([$($byte:expr),*], $bom:ident) => {
        assert_eq!(Bom::from(vec![$($byte as u8),*].as_slice()), Bom::$bom)
    }
}

#[test]
fn from_slice() {
    assert_slice!([], Null);

    assert_slice!([0, 0, 0xfe], Null);
    assert_slice!([0, 0, 0xfe, 0xfe], Null);
    assert_slice!([0, 0, 0xfe, 0xff], Utf32Be);

    assert_slice!([0x0e, 0xff], Null);
    assert_slice!([0x0e, 0xff, 0xfe], Null);
    assert_slice!([0x0e, 0xfe, 0xff], Scsu);

    assert_slice!([0x84, 0x31, 0x95], Null);
    assert_slice!([0x84, 0x31, 0x95, 0x32], Null);
    assert_slice!([0x84, 0x31, 0x95, 0x33], Gb18030);
    assert_slice!([0x84, 0x31, 0x95, 0x34], Null);

    assert_slice!([0x2b, 0x2f, 0x76], Null);
    assert_slice!([0x2b, 0x2f, 0x76, 0x37], Null);
    assert_slice!([0x2b, 0x2f, 0x76, 0x38], Utf7);
    assert_slice!([0x2b, 0x2f, 0x76, 0x39], Utf7);
    assert_slice!([0x2b, 0x2f, 0x76, 0x3a], Null);

    assert_slice!([0x2b, 0x2f, 0x76, 0x2a], Null);
    assert_slice!([0x2b, 0x2f, 0x76, 0x2b], Utf7);
    assert_slice!([0x2b, 0x2f, 0x76, 0x2c], Null);

    assert_slice!([0x2b, 0x2f, 0x76, 0x2e], Null);
    assert_slice!([0x2b, 0x2f, 0x76, 0x2f], Utf7);
    assert_slice!([0x2b, 0x2f, 0x76, 0x30], Null);

    assert_slice!([0xdd, 0x73, 0x66], Null);
    assert_slice!([0xdd, 0x73, 0x66, 0x72], Null);
    assert_slice!([0xdd, 0x73, 0x66, 0x73], UtfEbcdic);
    assert_slice!([0xdd, 0x73, 0x66, 0x74], Null);

    assert_slice!([0xef, 0xbb], Null);
    assert_slice!([0xef, 0xbb, 0xbe], Null);
    assert_slice!([0xef, 0xbb, 0xbf], Utf8);
    assert_slice!([0xef, 0xbb, 0xc0], Null);

    assert_slice!([0xf7, 0x64], Null);
    assert_slice!([0xf7, 0x64, 0x4b], Null);
    assert_slice!([0xf7, 0x64, 0x4c], Utf1);
    assert_slice!([0xf7, 0x64, 0x4d], Null);

    assert_slice!([0xfb, 0xee], Null);
    assert_slice!([0xfb, 0xee, 0x27], Null);
    assert_slice!([0xfb, 0xee, 0x28], Bocu1);
    assert_slice!([0xfb, 0xee, 0x29], Null);

    assert_slice!([0xfe], Null);
    assert_slice!([0xfe, 0xfe], Null);
    assert_slice!([0xfe, 0xff], Utf16Be);

    assert_slice!([0xff], Null);
    assert_slice!([0xff, 0xfd], Null);
    assert_slice!([0xff, 0xfe], Utf16Le);
    assert_slice!([0xff, 0xff], Null);

    assert_slice!([0xff, 0xfe, 0], Utf16Le);
    assert_slice!([0xff, 0xfe, 0, 0], Utf32Le);
    assert_slice!([0xff, 0xfe, 0, 1], Utf16Le);
}

#[test]
fn from_file() {
    let mut file = File::open("fixtures/ascii.txt").unwrap();
    assert_eq!(Bom::from(&mut file), Bom::Null);

    let mut file = File::open("fixtures/utf16-le.txt").unwrap();
    assert_eq!(Bom::from(&mut file), Bom::Utf16Le);
}

#[test]
fn from_path() {
    assert_eq!(Bom::from("fixtures/ascii.txt"), Bom::Null);
    assert_eq!(Bom::from("fixtures/utf16-le.txt"), Bom::Utf16Le);
}