Ejemplo n.º 1
0
        public void Known_But_NYI()
        {
            var alg = MultiBaseAlgorithm.Register("nyi", 'n');

            try
            {
                ExceptionAssert.Throws <NotImplementedException>(() => alg.Encode(null));
                ExceptionAssert.Throws <NotImplementedException>(() => alg.Decode(null));
            }
            finally
            {
                MultiBaseAlgorithm.Deregister(alg);
            }
        }
        /// <summary>
        ///   Register a new IPFS algorithm.
        /// </summary>
        /// <param name="name">
        ///   The name of the algorithm.
        /// </param>
        /// <param name="code">
        ///   The IPFS code assigned to thealgorithm.
        /// </param>
        /// <param name="encode">
        ///   A <c>Func</c> to encode a byte array.  If not specified, then a <c>Func</c> is created to
        ///   throw a <see cref="NotImplementedException"/>.
        /// </param>
        /// <param name="decode">
        ///   A <c>Func</c> to decode a string.  If not specified, then a <c>Func</c> is created to
        ///   throw a <see cref="NotImplementedException"/>.
        /// </param>
        /// <returns>
        ///   A new <see cref="MultiBaseAlgorithm"/>.
        /// </returns>
        public static MultiBaseAlgorithm Register(
            string name, char code,
            Func <byte[], string> encode = null,
            Func <string, byte[]> decode = null)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException("name");
            }
            if (Names.ContainsKey(name))
            {
                throw new ArgumentException(string.Format("The IPFS multi-base algorithm name '{0}' is already defined.", name));
            }
            if (Codes.ContainsKey(code))
            {
                throw new ArgumentException(string.Format("The IPFS multi-base algorithm code '{0}' is already defined.", code));
            }
            if (encode == null)
            {
                encode = (bytes) => { throw new NotImplementedException(string.Format("The IPFS encode multi-base algorithm '{0}' is not implemented.", name)); };
            }
            if (decode == null)
            {
                decode = (s) => { throw new NotImplementedException(string.Format("The IPFS decode multi-base algorithm '{0}' is not implemented.", name)); };
            }

            var a = new MultiBaseAlgorithm
            {
                Name   = name,
                Code   = code,
                Encode = encode,
                Decode = decode
            };

            Names[name] = a;
            Codes[code] = a;

            return(a);
        }
 /// <summary>
 ///   Remove an IPFS algorithm from the registry.
 /// </summary>
 /// <param name="algorithm">
 ///   The <see cref="MultiBaseAlgorithm"/> to remove.
 /// </param>
 public static void Deregister(MultiBaseAlgorithm algorithm)
 {
     Names.Remove(algorithm.Name);
     Codes.Remove(algorithm.Code);
 }
Ejemplo n.º 4
0
 public void Code_Already_Exists()
 {
     ExceptionAssert.Throws <ArgumentException>(() => MultiBaseAlgorithm.Register("base58btc-x", 'z'));
 }
Ejemplo n.º 5
0
 public void Bad_Name()
 {
     ExceptionAssert.Throws <ArgumentNullException>(() => MultiBaseAlgorithm.Register(null, '?'));
     ExceptionAssert.Throws <ArgumentNullException>(() => MultiBaseAlgorithm.Register("", '?'));
     ExceptionAssert.Throws <ArgumentNullException>(() => MultiBaseAlgorithm.Register("   ", '?'));
 }