Exemple #1
0
        /// <summary>
        /// Exports a mock of type T. The exported mock is then ready for importing.
        /// </summary>
        /// <remarks>
        /// Mock exports are always [Shared], i.e. only a single instance of a mock for a given contract name
        /// is created and reused during composition. You can create two different mocks of the same type
        /// if you use different contract names.
        /// </remarks>
        /// <typeparam name="T">The type of the mock to export.</typeparam>
        /// <param name="contractName">The contract name of the export.
        /// The CreationPolicySystem.ComponentModel.Composition.CreationPolicy metadata cannot be set.</param>
        /// <param name="metadata">Additional export metadata.</param>
        /// <returns>The exported mock instance. The returned value may be used
        /// to further arrange and assert expectations on the mock.</returns>
        public T ExportMock <T>(string contractName = null, IDictionary <string, object> metadata = null)
        {
            var type = typeof(T);

            var name = contractName ?? AttributedModelServices.GetContractName(type);

            List <Importable> contractImportables;

            if (!importables.TryGetValue(name, out contractImportables))
            {
                contractImportables = new List <Importable>();
                importables.Add(name, contractImportables);
            }

            var importable = contractImportables.FirstOrDefault(imp => imp.Metadata.DictionaryEquals(metadata));

            if (importable == null)
            {
                importable = new Importable {
                    Type = type
                };
                if (metadata != null)
                {
                    importable.Metadata = new Dictionary <string, object>(metadata);
                }
                contractImportables.Add(importable);
                this.GetExportedValues <T>(contractName).ToArray();
            }

            return((T)importable.Instance);
        }
Exemple #2
0
 private static object GetMock(Importable importable)
 {
     if (importable.Instance == null)
     {
         importable.Instance = Mock.Create(importable.Type);
     }
     return(importable.Instance);
 }
Exemple #3
0
        private void btnImport_Click(object sender, EventArgs e)
        {
            if (!EmulationState.instance.AssertWrite(segmentedAddress, 1))
            {
                return;
            }

            EmulationState.instance.RefreshROM();
            int numObjects = 0;

            foreach (TabPage page in tabImports.TabPages)
            {
                foreach (Control c in page.Controls[0].Controls)
                {
                    Importable import = c as Importable;
                    if (import != null)
                    {
                        if (import.PrepareForImport() == -1)
                        {
                            EmulationState.messages.AppendMessage("Import failed.", "Error");
                            return;
                        }
                        numObjects++;
                    }
                }
            }

            StringBuilder log    = new StringBuilder();
            int           cursor = segmentedAddress;

            cursor = globalsControl.Import(cursor);

            foreach (KeyValuePair <string, TextureImage> texture in textureLibrary.textures)
            {
                texture.Value.segmentOffset = cursor;
                texture.Value.WriteBytes(ref cursor);
                cursor = (cursor + 7) / 8 * 8;
            }
            int offsetInBank = segmentedAddress & 0xFFFFFF;

            EmulationState.RAMBank bank = EmulationState.instance.banks[segmentedAddress >> 0x18];
            Array.Copy(bank.value, offsetInBank, EmulationState.instance.ROM, bank.ROMStart + offsetInBank, cursor - segmentedAddress);

            foreach (TabPage page in tabImports.TabPages)
            {
                foreach (Control c in page.Controls[0].Controls)
                {
                    Importable import = c as Importable;
                    if (import != null)
                    {
                        int newCursor = import.Import(cursor);
                        if (newCursor == -1)
                        {
                            EmulationState.messages.AppendMessage("Import failed.", "Error");
                            return;
                        }
                        else if (newCursor != 0)
                        {
                            cursor = newCursor;
                        }
                    }
                }
            }
            File.WriteAllBytes(EmulationState.instance.ROMName, EmulationState.instance.ROM);
            PatchEngine.RecalculateChecksum();
            EmulationState.messages.AppendMessage("Imported " + numObjects + " objects.", "Info");
        }