mentat/edn/tests/serde_support.rs
Richard Newman 3dc68bcd38 Combine NamespacedKeyword and Keyword. (#689) r=nalexander
* Make properties on NamespacedKeyword/NamespacedSymbol private

* Use only a single String for NamespacedKeyword/NamespacedSymbol

* Review comments.

* Remove unsafe code in namespaced_name.

Benchmarking shows approximately zero change.

* Allow the types of ns and name to differ when constructing a NamespacedName.

* Make symbol namespaces optional.

* Normalize names of keyword/symbol constructors.

This will make the subsequent refactor much less painful.

* Use expect not unwrap.

* Merge Keyword and NamespacedKeyword.
2018-05-11 09:52:17 -07:00

57 lines
1.7 KiB
Rust

// Copyright 2018 Mozilla
//
// 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 http://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.
#![cfg(feature = "serde_support")]
extern crate serde_test;
extern crate serde_json;
extern crate edn;
use edn::symbols::Keyword;
use serde_test::{assert_tokens, Token};
#[cfg(feature = "serde_support")]
#[test]
fn test_serialize_keyword() {
let kw = Keyword::namespaced("foo", "bar");
assert_tokens(&kw, &[
Token::NewtypeStruct { name: "Keyword" },
Token::Struct { name: "NamespaceableName", len: 2 },
Token::Str("namespace"),
Token::Some,
Token::BorrowedStr("foo"),
Token::Str("name"),
Token::BorrowedStr("bar"),
Token::StructEnd,
]);
}
#[cfg(feature = "serde_support")]
#[test]
fn test_deserialize_keyword() {
let json = r#"{"name": "foo", "namespace": "bar"}"#;
let kw = serde_json::from_str::<Keyword>(json).unwrap();
assert_eq!(kw.name(), "foo");
assert_eq!(kw.namespace(), Some("bar"));
let bad_ns_json = r#"{"name": "foo", "namespace": ""}"#;
let not_kw = serde_json::from_str::<Keyword>(bad_ns_json);
assert!(not_kw.is_err());
let bad_ns_json = r#"{"name": "", "namespace": "bar"}"#;
let not_kw = serde_json::from_str::<Keyword>(bad_ns_json);
assert!(not_kw.is_err());
}