Skip to content

Instantly share code, notes, and snippets.

@jamesrcounts
Created August 8, 2012 15:29
Show Gist options
  • Select an option

  • Save jamesrcounts/3295947 to your computer and use it in GitHub Desktop.

Select an option

Save jamesrcounts/3295947 to your computer and use it in GitHub Desktop.
WinForms+EventApprovals
using System;
using System.Windows.Forms;
namespace ApprovalUtilities.Tests.Reflection
{
public partial class DemoForm : Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.CheckBox checkBox1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Timer timer1;
public DemoForm()
{
InitializeComponent();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void button1_Click(object sender, EventArgs e)
{
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
}
private void DemoForm_Load(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.button1 = new System.Windows.Forms.Button();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.listBox1 = new System.Windows.Forms.ListBox();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(13, 13);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// checkBox1
//
this.checkBox1.AutoSize = true;
this.checkBox1.Location = new System.Drawing.Point(192, 13);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(80, 17);
this.checkBox1.TabIndex = 1;
this.checkBox1.Text = "checkBox1";
this.checkBox1.UseVisualStyleBackColor = true;
this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);
//
// listBox1
//
this.listBox1.FormattingEnabled = true;
this.listBox1.Location = new System.Drawing.Point(13, 43);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(259, 212);
this.listBox1.TabIndex = 2;
//
// timer1
//
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// DemoForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.checkBox1);
this.Controls.Add(this.button1);
this.Name = "DemoForm";
this.Text = "DemoForm";
this.Load += new System.EventHandler(this.DemoForm_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion Windows Form Designer generated code
}
}
namespace ApprovalUtilities.Tests.Reflection
{
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using ApprovalTests;
using ApprovalUtilities.Reflection;
using ApprovalUtilities.Utilities;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class ExampleTest
{
[TestMethod]
[ApprovalTests.Reporters.UseReporter(typeof(ApprovalTests.Reporters.TortoiseDiffReporter))]
public void GetNonPublicFieldsAndSelf()
{
var testingForm = new DemoForm();
// Get all the non public instance FieldInfos
IEnumerable<FieldInfo> fieldInfos =
testingForm.NonPublicInstanceFields();
// Get the field values with their names, discard nulls, add the form
var objectDescriptors = fieldInfos
.Select(fi => new { fi.Name, Value = fi.GetValue(testingForm) })
.Concat(new[] { new { testingForm.Name, Value = (object)testingForm } })
.Where(fd => fd.Value != null);
// Associate fields with CallbackDescriptor collections, discard those
// without handlers
var objectsWithEvents = objectDescriptors
.Select(od => new { od.Name, Handlers = od.Value.GetPocoEvents().Concat(od.Value.GetEventHandlerListEvents()) })
.Where(od => od.Handlers.Any());
Approvals.VerifyAll(
objectsWithEvents,
item => item.Name + " => " + item.Handlers.ToReadableString());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment